JavaScript HTML DOM Node List

A node list is a collection of nodes

HTML DOM Node List

The getElementsByTagName() method returns a node list. A node list is an array-like collection of nodes.

The following code selects all <p> nodes in a document:


var x = document.getElementsByTagName("p");

The nodes can be accessed by an index number. To access the second <p> node you can write:

y = x[1];
Try it Yourself »

Note: The index starts at 0.

HTML DOM Node List Length

The length property defines the number of nodes in a node list:


var myNodelist = document.getElementsByTagName("p");
document.getElementById("demo").innerHTML = myNodelist.length;
Try it Yourself »

Example explained:

  1. Get all <p> elements in a node list
  2. Display the length of the node list

The length property is useful when you want to loop through the nodes in a node list:


Change the background color of all <p> elements in a node list:

Try it Yourself »