Select options Collection namedItem(id) - Get the text of the option with id="orange" in a drop-down list: - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Select

Description

Select options Collection namedItem(id) - Get the text of the option with id="orange" in a drop-down list:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form>
  <select id="mySelect">
    <option id="apple">Apple</option>
    <option id="orange">Orange</option>
    <option id="pineapple">Pineapple</option>
    <option id="banana">Banana</option>
  </select>
</form>//from   w w w . jav  a 2  s.  c  om

<button onclick="myFunction()">Test</button>

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

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

</body>
</html>

Related Tutorials