Javascript DOM HTML Element children Property get child text

Introduction

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

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

Click the button to get the text of the third child element (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 ww  w.j av a2  s .c om*/
<br><br>

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

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

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

</body>
</html>



PreviousNext

Related