Reputation: 495
Not sure whats going on here, I must be missing something. I have two select boxes and users can move elements between them to select which data elements they would like to download, here is the screen grab:
Everything works great EXCEPT when the user moves some elements back to the "Data Concepts" box leaving no elements in the "Selected Data" selected, as seen here:
Here is the code that is grabbing the values from the "Selected Data" box:
var ExportSel = [];
$('#DataList2').each(function(){
ExportSel = $('#DataList2').val();
});
But this returns an empty array if nothing is selected regardless of the number of elements in the "Selected Data" box. Although when I inspect it in firebug, it shows the elements and values are present in the selector box.
Am I misunderstanding the .each function, the .val function or something even more fundamental?
A huge thanks in advance, always impressed with the stack community!
Upvotes: 0
Views: 112
Reputation: 780851
The value of a <select multiple>
element is an array of all the selected options. But you don't care whether the options in the Selected Data
column are selected, you want an array of all of them. So you need to iterate over the options, getting their values.
var ExportSel = [];
$("#DataList2 option").each(function() {
ExportSel.push(this.value);
});
Upvotes: 1