NodeList
objects are collections of nodes such as those returned by properties such as Node.childNodes
and the document.querySelectorAll()
method.
Although NodeList
is not an Array
, it is possible to iterate on it using forEach()
. Several older browsers have not implemented this method yet. You can also convert it to an Array
using Array.from
.
In some cases, the NodeList
is a live collection, which means that changes in the DOM are reflected in the collection. For example, Node.childNodes
is live:
var parent = document.getElementById('parent'); var child_nodes = parent.childNodes; console.log(child_nodes.length); // let's assume "2" parent.appendChild(document.createElement('div')); console.log(child_nodes.length); // should output "3"
In other cases, the NodeList
is a static collection, meaning any subsequent change in the DOM does not affect the content of the collection. document.querySelectorAll()
returns a static NodeList
.
It's good to keep this distinction in mind when you choose how to iterate over the items in the NodeList
, and how you cache the length of the list in particular.
NodeList.length
NodeList
.NodeList.item()
null
if the index is out-of-bounds; can be used as an alternative to simply accessing nodeList[idx]
(which instead returns undefined
when idx
is out-of-bounds).NodeList.entries()
iterator
allowing to go through all key/value pairs contained in this object.NodeList.forEach()
NodeList
element.NodeList.keys()
iterator
allowing to go through all keys of the key/value pairs contained in this object.NodeList.values()
iterator
allowing to go through all values of the key/value pairs contained in this object.It's possible to loop over the items in a NodeList
using:
for (var i = 0; i < myNodeList.length; i++) { var item = myNodeList[i]; // Calling myNodeList.item(i) isn't necessary in JavaScript }
Don't be tempted to use for...in
or for each...in
to enumerate the items in the list, since that will also enumerate the length and item properties of the NodeList
and cause errors if your script assumes it only has to deal with element
objects. Also, for..in
is not guaranteed to visit the properties in any particular order.
for...of
loops will loop over NodeList
objects correctly:
var list = document.querySelectorAll( 'input[type=checkbox]' ); for (var item of list) { item.checked = true; }
Recent browsers also support iterator methods, forEach()
, as well as entries()
, values()
, and keys()
There is also an Internet Explorer compatible way to use Array.prototype.forEach
for iteration.
var list = document.querySelectorAll( 'input[type=checkbox]' ); Array.prototype.forEach.call(list, function (item) { item.checked = true; });
Specification | Status | Comment |
---|---|---|
DOM The definition of 'NodeList' in that specification. | Living Standard | |
Document Object Model (DOM) Level 3 Core Specification The definition of 'NodeList' in that specification. | Obsolete | |
Document Object Model (DOM) Level 2 Core Specification The definition of 'NodeList' in that specification. | Obsolete | |
Document Object Model (DOM) Level 1 Specification The definition of 'NodeList' in that specification. | Obsolete | Initial definition. |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
entries() , keys() , values() , forEach()
| 51 | No support | 50 (50) | No support | 38 | 10 (maybe prior) |
Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
entries() , keys() , values() , forEach()
| ? | ? | ? | 50.0 (50) | ? | No support | ? | 10 (maybe prior) | 51 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/NodeList