J

JavaScript Handbook

Clean • Professional

DOM NodeLists

2 minute

DOM NodeLists

A NodeList is an array-like collection of DOM nodes returned by certain DOM methods or properties. It allows you to access multiple nodes (elements, text nodes, comments, etc.) and perform operations like looping, reading, or modifying.

What is a NodeList?

A NodeList represents a list (collection) of nodes in the DOM tree.

You often get a NodeList when you use DOM methods like:

  • document.querySelectorAll()
  • document.childNodes
  • element.childNodes
  • document.getElementsByName()

Example:

<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>

<script>
const list = document.querySelectorAll("p");
console.log(list); // NodeList(3)
console.log(list[0].textContent); // "Paragraph 1"
</script>

Characteristics of NodeLists

FeatureDescription
TypeArray-like object (not a real array)
IndexingAccess elements using index numbers
Length.length property gives total nodes
IterationCan use for, for...of, or forEach()
Live/StaticMay be live or static depending on how it’s created

Live vs Static NodeLists

Static NodeList

  • Does not update automatically when the DOM changes.
  • Created by methods like querySelectorAll().

Live NodeList

  • Automatically updates when DOM nodes are added or removed.
  • Created by properties like childNodes.

Static NodeList Example

<ul id="list">
  <li>Apple</li>
  <li>Banana</li>
</ul>

<script>
const items = document.querySelectorAll("li"); // Static NodeList
console.log(items.length); // 2

// Add a new item
const newItem = document.createElement("li");
newItem.textContent = "Cherry";
document.getElementById("list").appendChild(newItem);

console.log(items.length); //  Still 2 (doesn't update)
</script>

Live NodeList Example

<ul id="list">
  <li>Dog</li>
  <li>Cat</li>
</ul>

<script>
const items = document.getElementById("list").childNodes; // Live NodeList
console.log(items.length); // 3 (includes text nodes)

const newItem = document.createElement("li");
newItem.textContent = "Bird";
document.getElementById("list").appendChild(newItem);

console.log(items.length); //  Updates automatically
</script>

Common Ways to Get a NodeList

Method/PropertyReturnsType
document.querySelectorAll(selector)All matching elementsStatic
element.childNodesAll child nodes (including text/comments)Live
document.getElementsByName(name)All elements with given nameLive
document.querySelectorAll()NodeList of matched elementsStatic

Accessing NodeList Elements

You can access NodeList elements by index or loop through them.

Access by Index

const list = document.querySelectorAll("p");
console.log(list[0].textContent);

Using for Loop

for (let i = 0; i < list.length; i++) {
  console.log(list[i].innerText);
}

Using for...of

for (let node of document.querySelectorAll("div")) {
  console.log(node.textContent);
}

Using forEach()

document.querySelectorAll("p").forEach((el, index) => {
  el.style.color = "blue";
  console.log(`Paragraph ${index + 1}: ${el.textContent}`);
});

NodeList vs HTMLCollection

FeatureNodeListHTMLCollection
ContainsAll node types (element, text, comment)Only element nodes
Live/StaticCan be live or staticAlways live
Supports forEach()YesNo
Returned byquerySelectorAll(), childNodesgetElementsByTagName(), children
Auto UpdatesOnly live NodeListsAlways updates

Converting NodeList to an Array

Sometimes you need to use array methods like .map(), .filter(), or .reduce().

You can easily convert a NodeList to an array:

Using Array.from()

const nodes = document.querySelectorAll("p");
const arr = Array.from(nodes);
arr.map(el => el.style.fontWeight = "bold");

Using Spread Operator

const arr = [...document.querySelectorAll("li")];

 

Article 0 of 0