Element lastElementChild Property - Javascript DOM

Javascript examples for DOM:Element lastElementChild

Description

The lastElementChild property returns the last child element of the specified element.

lastChild returns the last child node as an element node, a text node or a comment node depending on which one's last, while lastElementChild returns the last child node as an element node.

This property is read-only.

Return Value

A Node object, representing the last child element of an element, or null if there are no child elements

The following code shows how to get the HTML content of the last child element of an <ul> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<select id="mySelect" size="4">
  <option>Java</option>
  <option>SQL</option>
  <option>HTML</option>
  <option>CSS</option>
  </select>
<br><br>

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

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

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

</body>
</html>

Related Tutorials