Preparation Code Preparation HTML (this will be inserted in the <body>
of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <select multiple ="multiple" >
<option value ="1" > 1</option >
<option value ="2" > 2</option >
<option value ="3" selected ="selected" > 3</option >
<option value ="4" > 4</option >
<option value ="5" > 5</option >
<option value ="6" > 6</option >
<option value ="7" > 7</option >
<option value ="8" selected ="selected" > 8</option >
<option value ="9" > 9</option >
<option value ="10" > 10</option >
</select >
<script >
function getSelectValues1 (select ) {
var result = [];
var options = select && select.options ;
var opt;
for (var i=0 , iLen=options.length ; i<iLen; i++) {
opt = options[i];
if (opt.selected ) {
result.push (opt.value || opt.text );
}
}
return result;
}
function getSelectValues2 (select ) {
return Array .prototype .map .call (select.selectedOptions , function (a ) {return a.value ;});
}
function getSelectValues3 (select ) {
var result = [];
var options = select && select.selectedOptions ;
for (var i=0 , iLen=options.length ; i<iLen; i++) {
result.push (options[i].value || options[i].text );
}
return result;
}
</script >
Setup JS var select = document .querySelector ('select' ),
i, len, selected;
Teardown JS
delete select;
delete i;
delete len;
delete selected;