I am trying to convert a multiple select dropDown values (all options) into an array using jquery.
<select size="10" style="width: 330px;" name="itemList"
id="selectedItemLists" multiple="multiple">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value3</option>
<option value="4">value4</option>
</select>
Now using jquery how to create an array like array[0]=value1,array[1]=value2...
Please help me in this.
Thanks in advance.
Possibly something like:
var options = new Array();
$('#selectedItemLists > option:selected').each(
function(i){
options[i] = $(this).val();
});
JS Fiddle demo.
text()
instead of val()
:
var options = new Array();
$('#selectedItemLists > option:selected').each(
function(i){
options[i] = $(this).text();
});
JS Fiddle demo.
References:
:selected
selector.each()
.val()
.text()
.You can use .map()
to accomplish this as well.
var i = $.map($("#selectedItemLists option:selected"), function(elem){
return $(elem).text();
});
Example on jsfiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With