Javascript DOM HTML Select length Property get

Introduction

Return the number of <option> elements found in a drop-down list:

var x = document.getElementById("mySelect").length;

Click the button to return the number of option elements in the dropdown list.

View in separate window

<!DOCTYPE html>
<html>
<body>

<select id="mySelect">
  <option>Apple</option>
  <option>Pear</option>
  <option>Banana</option>
  <option>Orange</option>
</select>/*from   ww w .  jav  a2s  . co m*/

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.getElementById("mySelect").length;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The length property returns the number of <option> elements in a drop-down list.




PreviousNext

Related