HTML DOM NodeList item()
❮ The NodeList ObjectExamples
Get the <body> element's child nodes:
const nodeList = document.body.childNodes;
Try it Yourself »
Get the node name of the first child node:
const list = document.body.childNodes;
let name = list.item(0).nodeName;
Try it Yourself »
This produces the same result:
const list = document.body.childNodes;
let name = list[0].nodeName;
Try it Yourself »
Get the HTML content of the first <p> element in the document:
const list = document.getElementsByTagName("p");
let text = list.item(0).innerHTML;
Try it Yourself »
More examples below.
Description
The item()
method returns the node at a specified index in a
NodeList.
There are two ways to access a node at a specified index:
list.item(index)
or
list[index]
The easiest and most common method is [index].
See Also:
Syntax
nodelist.item(index)
or simply:
nodelist[index]
Parameters
Parameter | Description |
index | Required. The index of the node in the list. The nodes are sorted as they appear in the document. The index starts at 0. |
Return Value
Type | Description |
Object | The node at the specified index.null if the index is out of range. |
More Examples
Example
Get the HTML content of the first <p> element inside "myDIV":
const div = document.getElementById("myDIV");
const list = div.getElementsByTagName("p");
let text = list[0].innerHTML;
Try it Yourself »
Example
Change the HTML content of the first <p> element inside "myDIV":
const div = document.getElementById("myDIV");
const list = div.getElementsByTagName("p");
let text = list[0].innerHTML = "Paragraph changed";
Try it Yourself »
Example
Change the color of all elements with class="child":
const list = document.querySelectorAll(".child");
for (let i = 0; i < list.length; i++) {
list[i].style.color = "red";
}
Try it Yourself »
Browser Support
nodelist.item()
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