Node lastChild Property - Javascript DOM

Javascript examples for DOM:Node

Description

The lastChild property returns the last child node of the specified node, as a Node object.

lastChild returns the last child node as an element node, a text node or a comment node.

lastElementChild returns the last child node as an element node ignoring text and comment nodes.

Whitespace inside elements is considered as text, and text is considered as nodes.

This property is read-only.

Return Value

A Node object, representing the last child of a node, or null if there are no child nodes

The following code shows how to get the HTML content of the last child node 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() {//from   w  w w .  j a va  2 s  . c  om
    var x = document.getElementById("mySelect").lastChild.text;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials