Name
NodeList: a read-only array of nodes â DOM Level 1 Core: Object â NodeList
Properties
-
readonly unsigned long length
The number of nodes in the array.
Methods
-
item()
Returns the specified element of the array.
Description
The NodeList interface defines a read-only, ordered list (i.e., an array) of
Node objects. The length
property
specifies how many nodes are in the list, and the item( )
method allows you to obtain the
node at a specified position in the list. The elements of a NodeList
are always valid Node objects: NodeLists never contain null
elements.
In JavaScript, NodeList objects behave like JavaScript arrays,
and you can query an element from the list using square-bracket
array notation instead of calling the item(
)
method. However, you cannot assign new nodes to a
NodeList using square brackets. Since it is always easier to think
of a NodeList object as a read-only JavaScript array, this book uses
the notation Element[]
or
Node[]
(i.e., an Element array or
Node array) instead of NodeList. The methods Document.getElementsByTagName( )
, Element.getElementsByTagName( )
, and
HTMLDocument.getElementsByName( )
are all documented in this book as returning a Element[]
instead of a NodeList object.
Similarly, the childNodes
property of the Node object is technically a NodeList object, but
the Node
reference page defines
it as a Node[]
, and the property
itself is usually referred to as âthe childNodes[]
array.â
Note that NodeList objects are live: they are not static snapshots ...
Get JavaScript: The Definitive Guide, 5th 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.