Javascript Reference - HTML DOM Select length Property








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

Browser Support

length Yes Yes Yes Yes Yes

Syntax

var v = selectObject.length

Return Value

A Number type value representing the number of <option> elements found in the drop-down list.

Example

The following code shows how to get the number of <option> elements found in a drop-down list.


<!DOCTYPE html>
<html>
<body>
<!--  w w w .  ja v  a2 s.com-->
<select id="mySelect">
  <option>A</option>
  <option>B</option>
  <option>C</option>
  <option>D</option>
</select>
<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 code above is rendered as follows: