-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
DOM Navigation refers to the process of traversing and accessing elements in the Document Object Model (DOM) relative to other elements. It allows you to move up, down, or sideways in the DOM tree to access parents, children, and siblings of an element.
DOM Navigation is important for manipulating elements dynamically, especially when elements do not have IDs or classes, or when you want to handle relative elements.
DOM Navigation helps you to:
Access parent or child elements relative to a selected element.
Move to sibling elements for dynamic changes.
Traverse the DOM tree efficiently for event handling.
Build interactive UI components like menus, carousels, or forms dynamically.
Perform complex manipulations without relying solely on IDs or classes.
The DOM is structured like a tree, with nodes representing elements, text, or comments.
Parent Node – The element directly above a node.
Child Nodes – Elements nested inside a parent node.
Sibling Nodes – Elements at the same level (same parent).
Use parentNode
or parentElement
to access an element’s parent.
let child = document.querySelector(".child");
// Access parent element
let parent = child.parentElement;
// Change parent background color
parent.style.backgroundColor = "lightblue";
parentElement
ignores non-element nodes (like text nodes).
parentNode
includes all node types.
children
Propertylet parent = document.querySelector(".parent");
// Get all child elements
let children = parent.children;
// Change background of first child
children[0].style.backgroundColor = "yellow";
Returns HTMLCollection of child elements.
Only element nodes are included (ignores text nodes).
childNodes
Propertylet childNodes = parent.childNodes;
console.log(childNodes); // Includes text, comment, and element nodes
Useful if you want all nodes, not just elements.
let parent = document.querySelector(".parent");
// First element child
parent.firstElementChild.style.color = "red";
// Last element child
parent.lastElementChild.style.fontWeight = "bold";
firstElementChild
and lastElementChild
ignore text nodes.
firstChild
and lastChild
include all node types.
let element = document.querySelector(".middle");
// Next sibling element
let next = element.nextElementSibling;
next.style.border = "2px solid green";
// Previous sibling element
let prev = element.previousElementSibling;
prev.style.border = "2px solid blue";
nextElementSibling
and previousElementSibling
only consider element nodes.
nextSibling
and previousSibling
include all node types.
let element = document.querySelector(".middle");
console.log("Node name:", element.nodeName); // Returns tag name
console.log("Node type:", element.nodeType); // 1 = element node, 3 = text node
Useful when traversing nodes programmatically.
Node types help filter elements while navigating.
let child = document.querySelector(".child");
// Change parent background
child.parentElement.style.backgroundColor = "lightgray";
// Change previous sibling text color
child.previousElementSibling.style.color = "red";
// Change next sibling text color
child.nextElementSibling.style.color = "green";
This approach helps when you only have a reference to one element but need to affect nearby elements.
let parent = document.querySelector(".parent");
for (let i = 0; i < parent.children.length; i++) {
parent.children[i].style.padding = "10px";
parent.children[i].style.border = "1px solid black";
}
Use loops to apply styles or manipulate multiple children efficiently.
Useful for dynamic lists, tables, or menus.
<ul class="menu">
<li>Home</li>
<li class="active">About</li>
<li>Services</li>
<li>Contact</li>
</ul>
let activeItem = document.querySelector(".active");
// Change previous and next menu items
if (activeItem.previousElementSibling) {
activeItem.previousElementSibling.style.color = "gray";
}
if (activeItem.nextElementSibling) {
activeItem.nextElementSibling.style.color = "gray";
}
// Highlight parent UL
activeItem.parentElement.style.backgroundColor = "#f0f0f0";
Demonstrates navigation to parent, previous, and next siblings.
Useful for highlighting menu items or dynamic lists.
Prefer parentElement
, children
, firstElementChild
, and nextElementSibling
over properties that include text nodes for clarity.
Always check if a parent or sibling exists before modifying it to avoid errors.
Use loops and arrays for batch operations on multiple children.
Combine DOM navigation with event handling for dynamic interactions.
Minimize deep DOM traversal when possible for better performance.
JavaScript DOM Navigation allows you to traverse and access elements relative to a selected element. Key points:
Access parent elements using parentElement
.
Access child elements using children
or childNodes
.
Access first and last children with firstElementChild
and lastElementChild
.
Access siblings with nextElementSibling
and previousElementSibling
.
Loop through children for batch updates.
DOM navigation is essential for dynamic UI manipulation and responsive interactions.
Mastering DOM navigation enables you to efficiently manipulate elements without relying solely on IDs or classes, making your code more flexible and maintainable.
Select an element with a class .child
and change its parent element’s background color.
Access all child elements of a <div>
and change their font color to blue.
Get the first and last child of a <ul>
and make the text bold.
Select a middle element and change the background color of its previous and next siblings.
Loop through all children of a container and add a border around each element.
Select a list item and log the node name and node type of its parent.
Access the next sibling of an element and append text to it dynamically.
Traverse from a child element to its grandparent and change the grandparent’s background color.
Select an element and check if it has a previous sibling; if yes, change its text color.
Using a container element, loop through its children and change the background color of every alternate child.
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts