JS DOM मेथड्स
In this page:
document.createElement("tag");
parent.appendChild(element);
element.remove();
element.setAttribute("name", value);
getElementById और querySelector
getElementById() किसी single element को उसके exact id attribute से ढूंढता है, जबकि querySelector() किसी भी valid CSS selector से मैच करने वाला पहला element ढूंढता है, जो classes, attributes, या nested combinations जैसी कहीं ज़्यादा flexibility देता है।
उदाहरण: getElementById and querySelector
<p id="intro">Hello</p>
<script>
// Print `document.getElementById("intro")` to the console
console.log(document.getElementById("intro"));
// Print `document.querySelector(".missing, #intro")` to the console
console.log(document.querySelector(".missing, #intro"));
</script>
querySelectorAll
querySelectorAll() किसी दिए गए CSS selector से मैच करने वाला हर element एक NodeList के रूप में return करता है, जिसे forEach या एक standard for...of loop से हर matching element को process करने के लिए loop किया जा सकता है।
उदाहरण: querySelectorAll
<p class="item">A</p>
<p class="item">B</p>
<script>
// Call `document.querySelectorAll(".item").forEach(el => console.log(el.textContent))`
document.querySelectorAll(".item").forEach(el => console.log(el.textContent));
</script>
createElement और appendChild
createElement() memory में एक बिल्कुल नया element बनाता है, जो page पर तब तक नहीं दिखता जब तक उसे appendChild() से कहीं attach न किया जाए, जो उसे चुने गए parent element के आखिरी child के रूप में जोड़ता है।
उदाहरण: createElement and appendChild
<div id="container"></div>
<script>
// Declare the constant `p`, set to `document.createElement("p")`
const p = document.createElement("p");
// Assign "New paragraph" to `p.textContent`
p.textContent = "New paragraph";
// Call `document.getElementById("container").appendChild(p)`
document.getElementById("container").appendChild(p);
</script>
removeChild और innerHTML
removeChild() किसी खास child element को उसके parent से हटाता है, जिसके लिए आपको इसे parent पर ही कॉल करना होता है, जबकि innerHTML आपको एक ही step में किसी element के अंदर का markup पढ़ने या पूरी तरह बदलने देता है।
उदाहरण: removeChild and innerHTML
<div id="box"><span id="child">Remove me</span></div>
<script>
// Declare the constant `box`, set to `document.getElementById("box")`
const box = document.getElementById("box");
// Call `box.removeChild(document.getElementById("child"))`
box.removeChild(document.getElementById("child"));
// Assign "<p>Replaced content</p>" to `box.innerHTML`
box.innerHTML = "<p>Replaced content</p>";
</script>
classList और setAttribute
classList किसी element की CSS classes को manually class string parse किए बिना manage करने के लिए add(), remove(), और toggle() जैसे methods देता है, और setAttribute() किसी भी HTML attribute की value सीधे सेट करता है।
उदाहरण: classList and setAttribute
<div id="box" class="hidden"></div>
<script>
// Declare the constant `box`, set to `document.getElementById("box")`
const box = document.getElementById("box");
// Call `box.classList.remove("hidden")`
box.classList.remove("hidden");
// Call `box.classList.add("visible")`
box.classList.add("visible");
// Call `box.setAttribute("data-status", "active")`
box.setAttribute("data-status", "active");
</script>
- getElementById को शुरुआती # symbol के साथ इस्तेमाल करना, querySelector के उलट, getElementById सिर्फ plain id name expect करता है।
- यह भूल जाना कि querySelectorAll एक static NodeList return करता है, कोई live collection नहीं जो DOM बदलने पर automatically update हो।
- किसी element को गलत parent से removeChild करने की कोशिश करना, removeChild को हटाए जा रहे element के असली direct parent पर कॉल करना ज़रूरी है।
- getElementById, querySelector, और querySelectorAll हर एक DOM से elements select करते हैं, वे कौन से selectors accept करते हैं और कितने elements return करते हैं इसमें अलग होते हुए।
- createElement एक नया element बनाता है, और appendChild या removeChild उसे DOM tree से attach या detach करते हैं।
- innerHTML, classList, और setAttribute आपको किसी element का content, CSS classes, और attributes सीधे modify करने देते हैं।
यहाँ बताए गए सभी standard DOM methods हर modern browser में समान रूप से supported हैं और foundational, stable web platform features हैं।
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