children() DOM
Question:
Explain how the HTML element children
method works and give an example of its usage.
The children
method will return all child element nodes while .childNodes
returns all children including text nodes and comment nodes.
See the Pen children compared to childNodes by oscar (@nopity) on CodePen.
This difference is a big one also childNodes return a live list while children does not. One of the main ways the children is used is if you want to see how many children are in the body or on some element without having to target the element nodes.
var childrenOfBody = document.body.children.length;
// return all children of body not including the text or comment nodes