childNodes property

The childNodes property stores a pseudo-array of the element's child nodes (tags, comments and text nodes).

Syntax

javascriptelement.childNodes;

Example

We get all child nodes of an element and display their content:

html<div id="parent">text<p>parag</p><!--com--></div>
javascriptlet parent = document.querySelector('#parent');
let nodes = parent.childNodes;

for (let node of nodes) {
  console.log(node.textContent);
}

Example

Display a contents of the first node:

html<div id="parent">text<p>parag</p><!--com--></div>
javascriptlet parent = document.querySelector('#parent');
console.log(parent.childNodes[0].textContent);

The code execution result:

javascript'text'

Example

Let's display the contents of the node with the number 2:

html<div id="parent">text<p>parag</p><!--com--></div>
javascriptlet parent = document.querySelector('#parent');
console.log(parent.childNodes[2].textContent);

The code execution result:

javascript'com'

See also

  • the children property
    that contains child elements
  • the firstChild property
    that contains the first node
  • the lastChild property
    that contains the last node