25.222. NodeList: a read-only array of nodes DOM Level 1 Core: Object NodeList 25.222.1. Properties
readonly unsigned long length The number of nodes in the array. 25.222.2. Methods
item() Returns the specified element of the array. 25.222.3. 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 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! 25.222.4. See Also Document, Element |