Select length Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Select

Description

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

Return Value

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

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

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<select id="mySelect">
  <option>CSS</option>
  <option>HTML</option>
  <option>SQL</option>
  <option>Javascript</option>
</select>//w w  w  . ja  v  a  2 s . c  om

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

Related Tutorials