Javascript DOM HTML Node childNodes Property get the third child node

Introduction

Get the text of the third child node (index 2) of a <select> element:

var c = document.getElementById("mySelect").childNodes[2].text;

Click the button to get the text of the third child node (index 2) of the select element.

View in separate window

<!DOCTYPE html>
<html>
<body>
<select id="mySelect" size="4">
    <option>Python</option>
    <option>CSS</option>
    <option>C++</option>
    <option>Javascript</option>
</select>/*from  www.  ja  v  a  2 s. c  om*/
<br>
<br>

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

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

<script>
function myFunction() {
  var c = document.getElementById("mySelect").childNodes;
  document.getElementById("demo").innerHTML = c[2].text;
}
</script>

</body>
</html>



PreviousNext

Related