Reputation: 73
I would like to get back all the option selected of a multiple select when i submit my form.
like :
<select id="multipleSelect" multiple="multiple">
<option value="1">Text 1</option>
<option value="2">Text 2</option>
<option value="3">Text 3</option>
using somthing like that :
var selectedValues = document.getElementbyId('multipleSelect).values;
and get an array with all the value selected like ['2','3']
All that, without jQuery and in js or php is fine :)
thx!
Upvotes: 2
Views: 1149
Reputation: 4506
You can try like this,
var options = document.getElementById('multipleSelect').options;
var values = [];
var i = 0, len = options.length;
while (i < len)
{
if(options[i].selected){
values.push(options[i].value);
}
i++;
}
alert(values.join(', '));
Upvotes: 0
Reputation: 145388
var options = document.getElementById('multipleSelect').options,
result = [];
for (var i = 0, len = options.length; i < len; i++) {
var opt = options[i];
if (opt.selected) {
result.push(opt.value);
}
}
console.log(result);
DEMO: http://jsfiddle.net/AH2yK/
Upvotes: 2