Assigning select list values to array

arrays, html, javascript, selectlist

Solution

You can do this like :

JQuery-

var optionVal = new Array();
    $('select option').each(function() {
            optionVal.push($(this).val());
        });

Javascript-

var x = document.getElementById("mySelect"); 
var optionVal = new Array();
for (i = 0; i < x.length; i++) { 
    optionVal.push(x.options[i].text);
}

This stores all the options in your select box in the array `optionVal`.

Hope it helps you.

Problem

I have an array initialised in script ``` var listarray = new Array(); ``` And have a dynamically created select list --- ``` <select multiple size=6 width=150 style="width:150px" name="ToLB" > </select> ``` My question is how to assign all values of select list to the array. Thanks in advance.

Original source

Related problems