HTML DOM NodeList length
❮ The NodeList ObjectExamples
Get the number of child nodes in the document:
const nodeList = document.body.childNodes;
let number = nodeList.length;
Try it Yourself »
Get the <body> element's child nodes:
const nodeList = document.body.childNodes;
Try it Yourself »
Get the number of child nodes in "myDIV":
const element = document.getElementById("myDIV");
let numb = element.childNodes.length;
Try it Yourself »
More examples below.
Description
The length
property returns the number of nodes in a
NodeList.
The length
property is read-only.
See Also:
Syntax
nodelist.length
Return Value
Type | Description |
---|---|
Number | The number of nodes in the NodeList. |
More Examples
Example
How many <p> elements inside "myDIV":
const div = document.getElementById("myDIV");
const list = div.querySelectorAll("p");
let number = list.length;
Try it Yourself »
Example
Loop over all <p> elements in "myDIV" and change their font size:
const div = document.getElementById("myDIV");
const list = div.querySelectorAll("p");
for (let i = 0; i < list.length; i++) {
list[i].style.fontSize = "red";
}
Try it Yourself »
Example
Loop over all child nodes nodes and collect the name of each node:
const list = document.body.childNodes;
let text = "";
for (let i = 0; i < list.length; i++) {
text += list[i].nodeName + "<br>";
}
Try it Yourself »
Browser Support
nodelist.length
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
❮ The NodeList Object