JS DOM Collections
In this page:
document.getElementsByTagName("tag");
document.getElementsByClassName("class");
document.querySelectorAll("selector");
document.forms;
NodeList from querySelectorAll
document.querySelectorAll() returns a NodeList -- a static snapshot of matching elements at the moment the query ran, which does not automatically update if the DOM changes afterward, and which directly supports the forEach() method for iteration.
उदाहरण: NodeList from querySelectorAll
<p class="item">A</p><p class="item">B</p>
<script>
const list = document.querySelectorAll(".item"); // static NodeList
list.forEach(el => console.log(el.textContent));
</script>
HTMLCollection from getElementsByClassName/TagName
document.getElementsByClassName() and getElementsByTagName() return an HTMLCollection -- a "live" collection that automatically updates to reflect DOM changes as they happen, but which does not support forEach() directly, requiring conversion to an array first for most iteration needs.
उदाहरण: HTMLCollection from getElementsByClassName/TagName
<p class="item">A</p>
<script>
const collection = document.getElementsByClassName("item"); // live HTMLCollection
console.log(collection.length);
</script>
Converting Collections to Real Arrays
Array.from(collection) (or the spread syntax [...collection]) converts either a NodeList or an HTMLCollection into a genuine JavaScript array, unlocking the full array method set -- map, filter, reduce, and more -- that neither DOM collection type fully supports on its own.
उदाहरण: Converting Collections to Real Arrays
<p class="item">A</p><p class="item">B</p>
<script>
// Declare the constant `collection`, set to `document.getElementsByClassName("item")`
const collection = document.getElementsByClassName("item");
// Declare the constant `arr`, set to `Array.from(collection)`
const arr = Array.from(collection);
// Print `arr.map(el => el.textContent)` to the console
console.log(arr.map(el => el.textContent));
</script>
Choosing the Right Selection Method
querySelectorAll() with its flexible CSS-selector syntax and forEach-ready NodeList is the more modern, generally preferred default; getElementsByClassName/TagName remain useful specifically when you want a live, auto-updating collection that tracks DOM changes automatically.
उदाहरण: Choosing the Right Selection Method
<p class="item">A</p>
<script>
console.log(document.querySelectorAll(".item")); // modern default
console.log(document.getElementsByClassName("item")); // live collection
</script>
Common Collection Pitfalls
Two frequent mistakes: forgetting a collection might be empty (checking .length before assuming an element exists), and modifying the DOM while iterating a live HTMLCollection with a plain for loop, which can cause elements to be skipped or visited twice as the collection's length shifts mid-loop.
उदाहरण: Common Collection Pitfalls
<p class="item">A</p>
<script>
const items = document.querySelectorAll(".missing");
if (items.length === 0) console.log("No matching elements found"); // check length first
</script>
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic