Get Option Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Option

Introduction

The Option object represents an HTML <option> element.

You can access an <option> element by using getElementById():

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<select>
  <option id="myOption" value="css">CSS</option>
  <option value="java">Java</option>
</select>//w  w w .j a v  a  2s  .  c  o  m

<button onclick="myFunction()">get the text of the option in the drop-down list</button>

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

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

</body>
</html>

Related Tutorials