I have a select box.
<select id='x'>
<option value='1'>'Vineet'</option>
<option value='2'>'Vivek'</option>
<option value='3'>'Madhu'</option>
</select>
The options are added dynamically to it. At the end, I want to collect the values of ALL option elements contained within this select box into an array. I searched the forum for the answer, but Q/A are there for getting the ‘selected’ option only.
I tried this:
var myList = [];
$('#x').each(function() { myList.push( $(this).value() ) });
In firebug, It gives this error:
missing } after property list
But I fail to see any missing }. What am I doing wrong here?
You need to loop through each
optionelement within the select, not just the select itself, also, the correct method to get the value of an input in jQuery isval(), try this:Example fiddle
You can also use
map():In all the above cases the
myListvariable will contain an array of strings.