JS DOM कलेक्शन्स
In this page:
document.getElementsByTagName("tag");
document.getElementsByClassName("class");
document.querySelectorAll("selector");
document.forms;
querySelectorAll से NodeList
document.querySelectorAll() एक NodeList return करता है -- query चलने के समय matching elements का एक static snapshot, जो बाद में DOM बदलने पर अपने आप update नहीं होता, और जो iteration के लिए सीधे forEach() method support करता है।
उदाहरण: 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>
getElementsByClassName/TagName से HTMLCollection
document.getElementsByClassName() और getElementsByTagName() एक HTMLCollection return करते हैं -- एक "live" collection जो DOM बदलावों को होते ही अपने आप दिखाने लगता है, पर जो सीधे forEach() support नहीं करता, ज़्यादातर iteration ज़रूरतों के लिए पहले array में convert करना पड़ता है।
उदाहरण: HTMLCollection from getElementsByClassName/TagName
<p class="item">A</p>
<script>
const collection = document.getElementsByClassName("item"); // live HTMLCollection
console.log(collection.length);
</script>
Collections को असली Arrays में Convert करना
Array.from(collection) (या spread syntax [...collection]) किसी NodeList या HTMLCollection को एक असली JavaScript array में बदल देता है, जो पूरे array method set को unlock कर देता है -- map, filter, reduce, वगैरह -- जिसे न तो कोई DOM collection type खुद पूरी तरह support करता है।
उदाहरण: 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>
सही Selection Method चुनना
अपने flexible CSS-selector syntax और forEach-ready NodeList के साथ querySelectorAll() ज़्यादा modern, आमतौर पर preferred default है; getElementsByClassName/TagName खासतौर पर तब उपयोगी रहते हैं जब आपको एक live, auto-updating collection चाहिए जो DOM बदलावों को अपने आप track करे।
उदाहरण: 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>
आम Collection Pitfalls
दो आम गलतियां: यह भूल जाना कि collection खाली भी हो सकता है (किसी element के मौजूद होने का मान लेने से पहले .length जांचना), और किसी live HTMLCollection को एक साधारण for loop से iterate करते हुए DOM को बदलना, जिससे collection की length के loop के बीच में बदलने पर elements skip हो सकते हैं या दो बार visit हो सकते हैं।
उदाहरण: 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>
- यह मान लेना कि हर DOM collection .map() या .filter() जैसे array methods सीधे support करता है -- एक NodeList forEach support करता है, लेकिन एक HTMLCollection उसे भी बिना पहले convert किए support नहीं करता।
- यह न समझना कि एक HTMLCollection "live" है (automatically DOM changes reflect करता है), जबकि querySelectorAll से मिला एक NodeList "static" है (एक fixed snapshot) -- DOM modify करते हुए किसी live collection पर loop करना अनपेक्षित व्यवहार कर सकता है।
- array methods इस्तेमाल करने से पहले किसी collection को असली array में (Array.from() या spread operator से) convert करना भूल जाना, जिन्हें दोनों collection types natively support नहीं करते।
- querySelectorAll() एक static NodeList return करता है, जो forEach() support करता है लेकिन दूसरे array methods सीधे नहीं।
- getElementsByClassName() और getElementsByTagName() एक live HTMLCollection return करते हैं, जो forEach() भी बिल्कुल support नहीं करता।
- Array.from(collection) किसी भी DOM collection को एक असली array में convert करता है, array methods का पूरा set unlock करते हुए।
NodeList और HTMLCollection दोनों को सबसे शुरुआती DOM implementations से हर browser में support मिला है।
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