Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get multiple selected values and items from listbox using javascript

<!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.

like image 671
R J. Avatar asked Oct 08 '12 05:10

R J.


1 Answers

Replace

  if(x.options[i].selected ==true){
      alert(x.options[i].selected);
  }

with

  if(x.options[i].selected){
      alert(x.options[i].value);
  }
like image 136
Programming Guy Avatar answered Sep 29 '22 03:09

Programming Guy