Javascript DOM HTML Node lastChild Property get text

Introduction

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

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

Click the button to get the text of the last 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>//from  w ww .  j  av a  2 s  .co  m
<br><br>

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

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

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

</body>
</html>



PreviousNext

Related