Name

NodeList — a read-only array-like object of Nodes

Synopsis

A NodeList is a read-only array-like object whose elements are Node objects (usually Elements). The length property specifies how many nodes are in the list, and you can retrieve those nodes from indexes 0 through length-1. You can also pass the desired index to the item() method instead of indexing the NodeList directly. The elements of a NodeList are always valid Node objects: NodeLists never contain null elements.

NodeLists are commonly used: the childNodes property of Node, and the return values of Document.getElementsByTagName(), Element.getElementsByTagName(), and HTMLDocument.get Elements ByName() are all NodeLists, for example. Because NodeList is an array-like object, however, we often refer to those values informally as arrays, using language like “the child Nodes[] array.”

Note that NodeList objects are usually live: they are not static snapshots but immediately reflect changes to the document tree. For example, if you have a NodeList that represents the children of a specific node and you then delete one of those children, the child is removed from your NodeList. Be careful when you are looping through the elements of a NodeList: the body of your loop can make changes to the document tree (such as deleting nodes) that can affect the contents of the NodeList!

Properties

readonly unsigned long length

The number of nodes in the NodeList.

Methods

Node item(unsigned long index)

Returns the Node at the specified index

Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.