JavaScript – Get Last Child of an HTML Element

To get the last child of a specific HTML Element, using JavaScript, get reference to this HTML element, and read the lastElementChild property of this HTML Element.

lastElementChild property returns the last child of this HTML Element as Element object.

In the following example, we will get the last child of the HTML Element, which is selected by id "myElement", and change the font color of this last child to red.

example.html

Try this html file online, and click on the Click Me button. The script gets the last child of the HTML Element #myElement, and changes its color to red.

JavaScript - Get Last Child of an HTML Element
ADVERTISEMENT

Conclusion

In this JavaScript Tutorial, we learned how to get the last child of an HTML Element, using JavaScript.