Locked lesson.
About this lesson
How to retrieve an array-like collection of nodes.
Exercise files
Download this lesson’s related exercise files.
The Node List.docx59.3 KB The Node List - Solution.docx
59.7 KB
Quick reference
The Node List
A node list is a list of all HTML elements of a specific type, like all p tags.
When to use
Use the node list whenever you need to find all nodes of a particular type.
Instructions
To find all nodes of a type, for instance all p tags:
var myVar = document.getElementsByTagName("p");
To determine how many p's there are:
document.write(myVar.length);
To print them all out on the screen, loop through them:
var myCounter = 0;
while (myCounter < myVar.length) {
document.write(myVar[myCounter].innerHTML + "<br/>");
myCounter++;
}
Hints & tips
- A node list is a list of HTML elements of a specific type
- We can call .length to find out how many items are in the list
- We can loop through them to do certain actions
- The node list is not an actual array, but we can do some array-like things to it (like call .length)
Lesson notes are only available for subscribers.