Node: childNodes property
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The read-only childNodes property of the Node interface returns a live
NodeList of child nodes of the given element where
the first child node is assigned index 0. Child nodes include elements, text and
comments.
Note:
The NodeList being live means that its content is changed each time
new children are added or removed.
Browsers insert text nodes into a document to represent whitespace in the source markup.
Therefore a node obtained, for example, using Node.childNodes[0]
may refer to a whitespace text node rather than the actual element the author intended to get.
See Working with whitespace in the DOM for more information.
The items in the collection of nodes are objects, not strings. To get data from node
objects, use their properties. For example, to get the name of the first
childNode, you can use elementNodeReference.childNodes[0].nodeName.
The document object itself has two children: the Doctype declaration and the
root element, typically referred to as documentElement. In HTML
documents the latter is the <html> element.
It is important to keep in mind that childNodes includes all child nodes,
including non-element nodes like text and comment.
To get a collection containing only elements, use Element.children instead.
Value
A live NodeList containing the children of the node.
Note:
Several calls to childNodes return the same NodeList.
Examples
Simple usage
Remove all children from a node
Specifications
| Specification |
|---|
| DOM # ref-for-dom-node-childnodes① |