- Node: childNodes property
- Value
- Examples
- Simple usage
- Remove all children from a node
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- Getting Child Elements of a Node in JavaScript
- Get the first child element
- Get the last child element
- Get all child elements
- Summary
- How to get the children of an element using JavaScript
- You might also like.
Node: childNodes property
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.
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 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
// Note that parg is an object reference to aelement // First check that the element has child nodes if (parg.hasChildNodes()) let children = parg.childNodes; for (const node of children) // Do something with each child as children[i] // NOTE: List is live! Adding or removing children will change the list's `length` > >
Remove all children from a node
// This is one way to remove all children from a node // box is an object reference to an element while (box.firstChild) // The list is LIVE so it will re-index each call box.removeChild(box.firstChild); >
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Apr 7, 2023 by MDN contributors.
Your blueprint for a better internet.
Getting Child Elements of a Node in JavaScript
Summary: in this tutorial, you will learn how to get the first child element, last child element, and all children of a specified element.
Suppose that you have the following HTML fragment:
html>
html> head> meta charset="utf-8"> title>JS Get Child Elements title> head> body> ul id="menu"> li class="first">Home li> li>Products li> li class="current">Customer Support li> li>Careers li> li>Investors li> li>News li> li class="last">About Us li> ul> body> html>Code language: HTML, XML (xml)
Get the first child element
To get the first child element of a specified element, you use the firstChild property of the element:
let firstChild = parentElement.firstChild;
Code language: JavaScript (javascript)
If the parentElement does not have any child element, the firstChild returns null . The firstChild property returns a child node which can be any node type such as an element node, a text node, or a comment node. The following script shows the first child of the #menu element:
let content = document.getElementById('menu'); let firstChild = content.firstChild.nodeName; console.log(firstChild);
Code language: JavaScript (javascript)
#text
Code language: CSS (css)
- and
tags. This whitespace creates a #text node.
Note that any whitespace such as a single space, multiple spaces, returns, and tabs will create a #text node. To remove the #text node, you can remove the whitespaces as follows:
article id="content">
h2>Heading h2>p>First paragraph p> article>Code language: HTML, XML (xml)
Or to get the first child with the Element node only, you can use the firstElementChild property:
let firstElementChild = parentElement.firstElementChild;
Code language: JavaScript (javascript)
The following code returns the first list item which is the first child element of the menu:
let content = document.getElementById('menu'); console.log(content.firstElementChild);
Code language: JavaScript (javascript)
li class="first">
Home li>Code language: HTML, XML (xml)
- First, select the #menu element by using the getElementById() method.
- Second, get the first child element by using the firstElementChild property.
Get the last child element
To get the last child element of a node, you use the lastChild property:
let lastChild = parentElement.lastChild;
Code language: JavaScript (javascript)
In case the parentElement does not have any child element, the lastChild returns null . Similar to the the firstChild property, the lastChild property returns the first element node, text node, or comment node. If you want to select only the last child element with the element node type, you use the lastElementChild property:
let lastChild = parentElement.lastElementChild;
Code language: JavaScript (javascript)
The following code returns the list item which is the last child element of the menu:
let menu = document.getElementById('menu'); console.log(main.lastElementChild);
Code language: JavaScript (javascript)
li class="last">
About Us li>Code language: HTML, XML (xml)
Get all child elements
To get a live NodeList of child elements of a specified element, you use the childNodes property:
let children = parentElement.childNodes;
Code language: JavaScript (javascript)
The childNodes property returns all child elements with any node type. To get the child element with only the element node type, you use the children property:
let children = parentElement.children;
Code language: JavaScript (javascript)
The following example selects all child elements of the element with the Id main :
let menu = document.getElementById('menu'); let children = menu.children; console.log(children);
Code language: JavaScript (javascript)
Summary
- The firstChild and lastChild return the first and last child of a node, which can be any node type including text node, comment node, and element node.
- The firstElementChild and lastElementChild return the first and last child Element node.
- The childNodes returns a live NodeList of all child nodes of any node type of a specified node. The children return all child Element nodes of a specified node.
How to get the children of an element using JavaScript
To get all child nodes of an element, you can use the childNodes property. This property returns a collection of a node’s child nodes, as a NodeList object.
By default, the nodes in the collection are sorted by their appearance in the source code. You can use a numerical index (start from 0) to access individual nodes.
Let us say you have the following HTML code:
ul id="langs"> li>JavaScriptli> li>Nodeli> li>Javali> li>Rubyli> li>Rustli> ul>
- tag and print their content:
const ul = document.querySelector('#langs'); // get all children const childern = ul.childNodes; // iterate over all child nodes childern.forEach(li => console.log(li.innerText); >);
Here is how the output looks like:
undefined JavaScript undefined Node undefined Java undefined Ruby undefined Rust undefined
Wait, why undefined appears in the output?
This is because whitespace inside elements is considered as text, and text is treated as nodes. It also applies to comments that are considered as nodes too.
If you want to exclude comment and text nodes, use the children property instead. This property returns a collection of a node’s element nodes only, as an HTMLCollection object:
const children = ul.children; // iterate over all child nodes Array.from(children).forEach(li => console.log(li.innerText); >);
Here is how the output looks like now:
JavaScript Node Java Ruby Rust
The difference between childNodes and children is that childNodes returns a NodeList object containing all nodes, including text nodes and comment nodes, while children returns an HTMLCollection object only containing element nodes.
To get the first and last children of an element, JavaScript provides firstChild and lastChild properties:
const ul = document.querySelector('#langs'); // get first children const firstChild = ul.firstChild; // get last children const lastChild = ul.lastChild;
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.