NodeList
objects are collections of nodes, usually returned by properties such as Node.childNodes
and methods such as document.querySelectorAll()
.
Although NodeList
is not an Array
, it is possible to iterate over it with forEach()
. It can also be converted to a real Array
using Array.from()
.
However, some older browsers have not implemented NodeList.forEach()
nor Array.from()
. This can be circumvented by using Array.prototype.forEach()
— see this document's Example.
In some cases, the NodeList
is live, which means that changes in the DOM automatically update 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); // outputs "3"
In other cases, the NodeList
is static, where any changes 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 if you should cache the list's length.
NodeList.length
NodeList
.NodeList.item()
null
if the index is out-of-bounds.nodeList[i]
(which instead returns undefined
when i
is out-of-bounds). This is mostly useful for non-JavaScript languages DOM implementations.NodeList.entries()
iterator
, allowing code to go through all key/value pairs contained in the collection. (In this case, the keys are numbers starting from 0 and the values are nodes.)NodeList.forEach()
NodeList
element, passing the element as an argument to the function.NodeList.keys()
iterator
, allowing code to go through all the keys of the key/value pairs contained in the collection. (In this case, the keys are numbers starting from 0.)NodeList.values()
iterator
allowing code to go through all values (nodes) of the key/value pairs contained in the collection.It's possible to loop over the items in a NodeList
using a for loop:
for (var i = 0; i < myNodeList.length; i++) { var item = myNodeList[i]; }
Don't use for...in
or for each...in
to enumerate the items in NodeList
s, since they will also enumerate its length
and item
properties 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 checkbox of list) { checkbox.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 (checkbox) { checkbox.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. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | Yes | Yes | Yes | Yes | Yes |
length |
1 | 12 | ? | ? | ? | ? |
entries |
51 | 16 | 50 | No | 38 | 10 |
forEach |
51 | 16 | 50 | No | 38 | 10 |
item |
1 | 12 | ? | ? | ? | ? |
keys |
51 | 16 | 50 | No | 38 | 10 |
values |
51 | 16 | 50 | No | 38 | 10 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
length |
Yes | Yes | ? | ? | ? | ? | Yes |
entries |
51 | 51 | ? | 50 | ? | 10 | 5.0 |
forEach |
51 | 51 | ? | 50 | ? | 10 | 5.0 |
item |
Yes | Yes | ? | ? | ? | ? | Yes |
keys |
51 | 51 | ? | 50 | ? | 10 | 5.0 |
values |
51 | 51 | ? | 50 | ? | 10 | 5.0 |
© 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