Javascript DOM HTML Select Object get

Introduction

The Select object represents an HTML <select> element.

We can access a <select> element via document.getElementById():

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

Click the button to get the number of option elements found in a drop-down list.

View in separate window

<!DOCTYPE html>
<html>
<body>
<select id="mySelect" size="4">
  <option>Apple</option>
  <option>Pear</option>
  <option>Banana</option>
  <option>Orange</option>
</select>/*from w  w  w  .j a v  a2  s .  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>



PreviousNext

Related