I have an html form. After submittal I need to have the selected item(s) for each form control remain selected. This works for radio buttons, checkboxes, drop-down lists, but not for my multiselect listbox. I also need this ability so I can pass the form data to a jquery function to perform other tasks. The process works on other pages without multiselect listboxes. I am using Django server-side and normally do a 'for loop' or 'if statement' to check user selections. I can get the selection as a list from my javascript, but in the Chrome network panel, it gives the list items as separate form variables. What is the method to get this formatting?
Update The jfiddle provided by meni181818 should work and I think it is the correct direction. However, I have updated my page to only include this code and it doesn't work. I can copy the code into a jfiddle and have it work, but not on my page. I'm not sure if it's a Django issue. The only additional code on the page is:
<head>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>
you can get all the selected items as array:
arrVal = $("select").val();
and to JSON:
JSON.stringify(arrVal);
if you want you can return it as JSON, and parse it to array with js\jq:
var arrJson = JSON.parse(THE_JSON_STRING);
and set the selected items:
$("select").val(arrJson);
get & set demo: http://jsfiddle.net/ZA82M/2/