JavaScript – Get Children of an HTML Element

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

children property returns HTMLCollection object which contains the child elements in this HTML Element.

In the following example, we will get the children elements of the HTML Element which is selected by id "myElement".

example.html

We can access each of the child in element.children using index, as shown in the following example.

example.html

Try this html file online, and click on the Click Me button. Children of the element with id myElement are accessed using index and printed to the console output as shown in the following screenshot.

JavaScript - Get Children of an HTML Element
ADVERTISEMENT

Conclusion

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