I think this is a good opportunity to use the Traversing / map method:
var valuesArray = $("#selectId option").map(function(){ return this.value; }).get();
And if you want to get two separate arrays containing selected and unselected values, you can do something like this:
var values = { selected: [], unselected:[] }; $("#selectId option").each(function(){ values[this.selected ? 'selected' : 'unselected'].push(this.value); });
After that, arrays of values.selected and values.unselected will contain the correct elements.
CMS
source share