JS DOM नेविगेशन
In this page:
element.parentNode;
element.children;
element.firstElementChild;
element.nextElementSibling;
element.previousElementSibling;
Parent Element तक Navigate करना
.parentElement DOM tree में किसी element के सीधे parent को return करता है -- यह ऊपर की ओर जाने के लिए उपयोगी है, जैसे किसी clicked बटन के containing card या list item को ढूंढना, बिना उस ancestor को सीधे select किए।
उदाहरण: Navigating to a Parent Element
<div id="parent"><button id="child">Click</button></div>
<script>
// Declare the constant `child`, set to `document.getElementById("child")`
const child = document.getElementById("child");
// Print `child.parentElement.id` to the console
console.log(child.parentElement.id);
</script>
Child Elements तक Navigate करना
.children किसी element के सीधे child elements (text और comment nodes को छोड़कर) का एक live collection return करता है, और .firstElementChild / .lastElementChild उन children के पहले या आखिरी तक सीधे जाते हैं, पूरे collection में index किए बिना।
उदाहरण: Navigating to Child Elements
<ul id="list"><li>A</li><li>B</li></ul>
<script>
// Declare the constant `list`, set to `document.getElementById("list")`
const list = document.getElementById("list");
// Print `list.children.length` to the console
console.log(list.children.length);
// Print `list.firstElementChild.textContent` to the console
console.log(list.firstElementChild.textContent);
// Print `list.lastElementChild.textContent` to the console
console.log(list.lastElementChild.textContent);
</script>
Sibling Elements तक Navigate करना
.nextElementSibling और .previousElementSibling उसी tree level पर पास वाले element तक जाते हैं, किसी भी दिशा में -- यह relative operations के लिए उपयोगी है, जैसे user के click किए हुए item के ठीक बाद वाले item को highlight करना।
उदाहरण: Navigating to Sibling Elements
<ul><li id="first">A</li><li id="second">B</li></ul>
<script>
// Declare the constant `first`, set to `document.getElementById("first")`
const first = document.getElementById("first");
// Print `first.nextElementSibling.textContent` to the console
console.log(first.nextElementSibling.textContent);
</script>
Event Target के Relative Navigate करना
event.target (जिस element ने event trigger किया) को navigation properties के साथ मिलाना आपको हर चीज़ के लिए अलग IDs के बिना related elements ढूंढने और बदलने देता है -- जैसे किसी delete बटन के containing list item को ढूंढकर उसे हटाना, सिर्फ tree relationships से काम करते हुए।
उदाहरण: Navigating Relative to an Event Target
<ul>
<li>Item <button class="del">X</button></li>
</ul>
<script>
document.querySelector(".del").addEventListener("click", (e) => {
// Print `e.target.parentElement.textContent` to the console
console.log(e.target.parentElement.textContent);
});
</script>
Navigation बनाम Re-querying: कौन सा इस्तेमाल करें
Navigation properties तब आदर्श हैं जब आपके पास पहले से किसी पास वाले element का reference हो और बस उसके relative जाना हो -- document root से querySelector() फिर से चलाना तब ज़्यादा सही है जब आपको ऐसा कुछ ढूंढना हो जो आपके मौजूदा किसी reference से असंबंधित हो।
उदाहरण: Navigation vs Re-querying: Which to Use
<div id="card"><button id="btn">Click</button></div>
<script>
document.getElementById("btn").parentElement;
// Print `document.querySelector("#card")` to the console
console.log(document.querySelector("#card"));
</script>
- non-Element-specific properties (childNodes, nextSibling) इस्तेमाल करना जब आप specifically element-only navigation चाहते हैं, क्योंकि उनमें text nodes (जैसे whitespace) भी शामिल होते हैं जिन्हें childNodes और nextSibling filter नहीं करते।
- यह मान लेना कि कोई navigation property हमेशा कुछ return करेगी -- last child पर .nextElementSibling, या document root पर .parentElement, null return करते हैं, और code को यह handle करना चाहिए।
- querySelector से DOM को दोबारा query करना जब एक simple navigation property (जैसे .parentElement) कम overhead के साथ same element ढूंढ सकती।
- parentElement, children, firstElementChild, और lastElementChild related elements तक navigate करते हैं, text/comment nodes को skip करते हुए।
- nextElementSibling और previousElementSibling same tree level पर adjacent sibling elements तक जाते हैं।
- उस दिशा में कुछ न होने पर ये properties null return कर सकती हैं, इसलिए result इस्तेमाल करने से पहले check करना मायने रखता है।
सभी Element-based DOM navigation properties हर modern browser में supported हैं।
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