Javascript DOM HTML Option Object get

Introduction

The Option object represents an HTML <option> element.

We can access an <option> element via document.getElementById():

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

Click the button to get the text of the Javascript option in the drop-down list.

View in separate window

<!DOCTYPE html>
<html>
<body>
<select>
  <option id="myOption" value="javascript">Javascript</option>
  <option value="html">HTML</option>
</select>/*  w w w .j  a va  2 s  .  c o  m*/
<button onclick="myFunction()">Test</button>

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

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

</body>
</html>



PreviousNext

Related