Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

NodeList

BaselineWidely available

NodeList objects are collections ofnodes, usually returned by properties such asNode.childNodes and methods such asdocument.querySelectorAll().

This interface was anattempt to create an unmodifiable list and only continues to be supported to not break code that's already using it. Modern APIs represent list structures using types based on JavaScriptarrays, thus making many array methods available, and at the same time imposing additional semantics on their usage (such as making their items read-only).

These historical reasons do not mean that you as a developer should avoidNodeList. You don't createNodeList objects yourself, but you get them from APIs such asDocument.querySelectorAll(), and these APIs are not deprecated. However, be careful of the semantic differences from a real array.

AlthoughNodeList is not anArray, it is possible to iterate over it withforEach(). It can also be converted to a realArray usingArray.from().

Live vs. Static NodeLists

Although they are both consideredNodeList objects, there are 2 varieties of NodeList:live andstatic.

Live NodeLists

In some cases, theNodeList islive, which means that changes in the DOM automatically update the collection.

For example,Node.childNodes is live:

js
const parent = document.getElementById("parent");let childNodes = parent.childNodes;console.log(childNodes.length); // let's assume "2"parent.appendChild(document.createElement("div"));console.log(childNodes.length); // outputs "3"

Static NodeLists

In other cases, theNodeList isstatic, where any changes in the DOM do not affect the content of the collection. The ubiquitousdocument.querySelectorAll() method returns astaticNodeList.

It's good to keep this distinction in mind when you choose how to iterate over the items in theNodeList, and whether you should cache the list'slength.

Instance properties

NodeList.lengthRead only

The number of nodes in theNodeList.

Instance methods

NodeList.item()

Returns an item in the list by its index, ornull if the index is out-of-bounds.

An alternative to accessingnodeList[i] (which instead returnsundefined wheni is out-of-bounds). This is mostly useful for non-JavaScript DOM implementations.

NodeList.entries()

Returns aniterator, allowing code to go through all key/value pairs contained in the collection. (In this case, the keys are integers starting from0 and the values are nodes.)

NodeList.forEach()

Executes a provided function once perNodeList element, passing the element as an argument to the function.

NodeList.keys()

Returns aniterator, allowing code to go through all the keys of the key/value pairs contained in the collection. (In this case, the keys are integers starting from0.)

NodeList.values()

Returns aniterator allowing code to go through all values (nodes) of the key/value pairs contained in the collection.

Example

It's possible to loop over the items in aNodeList using afor loop:

js
for (let i = 0; i < myNodeList.length; i++) {  let item = myNodeList[i];}

Don't usefor...in to enumerate the items inNodeLists, since they willalso enumerate itslength anditem properties and cause errors if your script assumes it only has to deal withelement objects. Also,for...in is not guaranteed to visit the properties in any particular order.

for...of loops overNodeList objects correctly:

js
const list = document.querySelectorAll("input[type=checkbox]");for (const checkbox of list) {  checkbox.checked = true;}

Browsers also support the iterator method (forEach()) as well asentries(),values(), andkeys().

Specifications

Specification
DOM
# interface-nodelist

Browser compatibility

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp