<!DOCTYPE html>
<html>
<script>
function getValue()
{
var x=document.getElementById("sel");
for (var i = 0; i < x.options.length; i++) {
if(x.options[i].selected ==true){
alert(x.options[i].selected);
}
}
}
</script>
</head>
<body>
<select multiple="multiple" id="sel">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="button" value="Get Value" onclick="getValue()"/>
</body>
</html>
This is my code. how do i get all the selected values from listbox using javascript.The above code showing show true for all selected value.
Replace
if(x.options[i].selected ==true){
alert(x.options[i].selected);
}
with
if(x.options[i].selected){
alert(x.options[i].value);
}
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