Javascript DOM HTML Element lastElementChild Property get tag text

Introduction

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

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

Click the button to get the text of the last child element 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>
<br><br>

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

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

<script>
function myFunction() {//from www.  j av  a 2 s .c  om
  var x = document.getElementById("mySelect").lastElementChild.text;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related