JS DOM Navigation
In this page:
element.parentNode;
element.children;
element.firstElementChild;
element.nextElementSibling;
element.previousElementSibling;
Navigating to a Parent Element
.parentElement returns an element's direct parent in the DOM tree -- useful for walking upward, like finding the containing card or list item of a button that was clicked, without needing to have selected that ancestor directly.
उदाहरण: 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>
Navigating to Child Elements
.children returns a live collection of an element's direct child elements (excluding text and comment nodes), and .firstElementChild / .lastElementChild jump directly to the first or last of those children, without needing to index into the full collection.
उदाहरण: 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>
Navigating to Sibling Elements
.nextElementSibling and .previousElementSibling move to the adjacent element at the same tree level, in either direction -- useful for relative operations, like highlighting the item right after the one a user clicked.
उदाहरण: 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>
Navigating Relative to an Event Target
Combining event.target (the element that triggered an event) with navigation properties lets you find and manipulate related elements without needing separate IDs for everything -- like finding a delete button's containing list item to remove it, working purely from 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 vs Re-querying: Which to Use
Navigation properties are ideal when you already have a reference to a nearby element and just need to move relative to it -- re-running querySelector() from the document root makes more sense when you need to find something unrelated to any element you currently hold a reference to.
उदाहरण: 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>
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