You are here: Reference > JavaScript > client-side > HTML DOM > properties > lastChild

lastChild property

Browser support:
Returns a reference to the last child of the current element.
The childNodes collection contains the direct descendants of an element, in source order. The firstChild property retrieves the first item from the childNodes collection. Similarly, the lastChild property returns the last item from the childNodes collection. The childNodes collection also contains the text and comment nodes, not only HTML elements. If you only need those HTML elements that are direct descendants of an element, use the children collection.

Syntax:

object.lastChild;
You can find the related objects in the Supported by objects section below.
This property is read-only.

Possible values:

Reference to the last child.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the lastChild property and childNodes collection:
<head>
    <script type="text/javascript">
        function GetLastValue () {
            var selectObj = document.getElementById ("mySelect");
            alert (selectObj.lastChild.text);
            // other way to get the last child
            alert (selectObj.childNodes[childNodes.length - 1].text);
        }
    </script>
</head>
<body>
    <button onclick="GetLastValue ();">Get the value of the last child!</button>
    <select id="mySelect" multiple="multiple">
        <option value="0">Apple</option>
        <option>Peach</option>
        <option>Pear</option></select>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content