Javascript Reference - HTML DOM Node lastChild Property








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

Browser Support

lastChild Yes Yes Yes Yes Yes

Syntax

var v = node.lastChild

Return Value

The last child of the node as a Node object.





Example

The following code shows how to get the last child node in a list.


<!DOCTYPE html>
<html>
<body>
<p>text</p>
<ul id="myList"><li>A</li><li>B</li></ul>
<!--  w w  w.  java 2 s . c  o  m-->
<p id="demo">this is a test</p>

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

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

</body>
</html>

The code above is rendered as follows: