Javascript DOM HTML Node firstChild Property

Introduction

Get the text of the first child node of a <select> element:

var x = document.getElementById("mySelect").firstChild.text;

Click the button to get the text of the first child node 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>//  www .jav a2  s  .c o  m
<br>
<br>

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

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

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

</body>
</html>



PreviousNext

Related