What is the Difference Between HTMLCollection and NodeList, and How to Obtain Them
- 409Words
- 2Minutes
- 08 Sep, 2024
In web development, HTMLCollection and NodeList are common DOM object collections used to store multiple DOM elements. Although they seem similar, there are significant differences in behavior and characteristics. Understanding these differences is crucial for efficiently manipulating DOM elements.
1. HTMLCollection
HTMLCollection is a live-updating collection. When the DOM structure changes, HTMLCollection automatically reflects those changes. It only contains element nodes, so it will not store other types of nodes like text or comment nodes.
Features
- Live updating:
HTMLCollectionautomatically updates when the DOM structure changes. - Multiple access methods: Elements can be accessed by index,
lengthproperty, or by theiridornameattributes. - Contains only element nodes: Ignores text and comment nodes.
Methods to obtain
document.getElementsByTagName(tagName): Retrieves a collection of elements by tag name.document.getElementsByClassName(className): Retrieves a collection of elements by class name.document.forms,document.images,document.links: These also return anHTMLCollection.
1const divs = document.getElementsByTagName("div"); // Returns an HTMLCollection2. NodeList
NodeList is a collection that can contain all types of nodes (element, text, comment, etc.). It can be either static (i.e., not updated as the DOM changes) or live, depending on how it is obtained.
Features
- Non-live updating: A
NodeListobtained viaquerySelectorAllis static and does not update automatically. - Contains all node types: In addition to element nodes, it can store text, comment, and other types of nodes.
- Supports multiple iteration methods:
NodeListcan be iterated usingforEach()(supported in modern browsers).
Methods to obtain
document.querySelectorAll(selector): Returns a staticNodeList.document.childNodes: Returns a liveNodeListcontaining all child nodes (elements, text, comments, etc.).parentNode.childNodes: Retrieves all child nodes of a node, returning aNodeList.
1const divs = document.querySelectorAll("div"); // Returns a static NodeListKey Differences Summary
-
Node Types:
HTMLCollectiononly contains element nodes.NodeListcan contain all types of nodes (element, text, comment, etc.).
-
Live Updating:
HTMLCollectionis live and updates automatically.NodeListobtained viaquerySelectorAll()is static, whilechildNodesis live.
-
Iteration Methods:
HTMLCollectioncan be accessed by index but does not supportforEach().NodeListsupportsforEach()and is more flexible for iteration.
Selection Advice
- When you need to manipulate a dynamic DOM structure,
HTMLCollectionis more suitable as it automatically updates with changes to the DOM. - If you only need a one-time static collection of elements,
NodeListis the better choice, especially the staticNodeListobtained viaquerySelectorAll().
Code Examples
1// Obtain HTMLCollection2let htmlCollection = document.getElementsByClassName("example");3console.log(htmlCollection); // A live-updating collection4
5// Obtain NodeList6let nodeList = document.querySelectorAll(".example");7console.log(nodeList); // A static collection8
9// Iterate through NodeList10nodeList.forEach((element) => {11 console.log(element);12});By understanding the differences between HTMLCollection and NodeList, developers can more flexibly manipulate the DOM, improving both code performance and maintainability.