
- Javascript Basics Tutorial
- Javascript - Home
- JavaScript - Roadmap
- JavaScript - Overview
- JavaScript - Features
- JavaScript - Enabling
- JavaScript - Placement
- JavaScript - Syntax
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - Comments
- JavaScript - Variables
- JavaScript - let Statement
- JavaScript - Constants
- JavaScript - Data Types
- JavaScript - Type Conversions
- JavaScript - Strict Mode
- JavaScript - Reserved Keywords
- JavaScript Operators
- JavaScript - Operators
- JavaScript - Arithmetic Operators
- JavaScript - Comparison Operators
- JavaScript - Logical Operators
- JavaScript - Bitwise Operators
- JavaScript - Assignment Operators
- JavaScript - Conditional Operators
- JavaScript - typeof Operator
- JavaScript - Nullish Coalescing Operator
- JavaScript - Safe Assignment Operator
- JavaScript - Delete Operator
- JavaScript - Comma Operator
- JavaScript - Grouping Operator
- JavaScript - Yield Operator
- JavaScript - Spread Operator
- JavaScript - Exponentiation Operator
- JavaScript - Operator Precedence
- JavaScript Control Flow
- JavaScript - If...Else
- JavaScript - While Loop
- JavaScript - For Loop
- JavaScript - For...in
- Javascript - For...of
- JavaScript - Loop Control
- JavaScript - Break Statement
- JavaScript - Continue Statement
- JavaScript - Switch Case
- JavaScript - User Defined Iterators
- JavaScript Functions
- JavaScript - Functions
- JavaScript - Function Expressions
- JavaScript - Function Parameters
- JavaScript - Default Parameters
- JavaScript - Function() Constructor
- JavaScript - Function Hoisting
- JavaScript - Self-Invoking Functions
- JavaScript - Arrow Functions
- JavaScript - Function Invocation
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - Closures
- JavaScript - Variable Scope
- JavaScript - Global Variables
- JavaScript - Smart Function Parameters
- JavaScript Objects
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - Strings
- JavaScript - Arrays
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Handler
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Sets
- JavaScript - WeakSet
- JavaScript - Maps
- JavaScript - WeakMap
- JavaScript - Iterables
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - Template Literals
- JavaScript - Tagged Templates
- Object Oriented JavaScript
- JavaScript - Objects
- JavaScript - Classes
- JavaScript - Object Properties
- JavaScript - Object Methods
- JavaScript - Static Methods
- JavaScript - Display Objects
- JavaScript - Object Accessors
- JavaScript - Object Constructors
- JavaScript - Native Prototypes
- JavaScript - ES5 Object Methods
- JavaScript - Encapsulation
- JavaScript - Inheritance
- JavaScript - Abstraction
- JavaScript - Polymorphism
- JavaScript - Destructuring
- JavaScript - Destructuring Assignment
- JavaScript - Object Destructuring
- JavaScript - Array Destructuring
- JavaScript - Nested Destructuring
- JavaScript - Optional Chaining
- JavaScript - Global Object
- JavaScript - Mixins
- JavaScript - Proxies
- JavaScript Versions
- JavaScript - History
- JavaScript - Versions
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript Asynchronous
- JavaScript - Asynchronous
- JavaScript - Callback Functions
- JavaScript - Promises
- JavaScript - Async/Await
- JavaScript - Microtasks
- JavaScript - Promisification
- JavaScript - Promises Chaining
- JavaScript - Timing Events
- JavaScript - setTimeout()
- JavaScript - setInterval()
- JavaScript Cookies
- JavaScript - Cookies
- JavaScript - Cookie Attributes
- JavaScript - Deleting Cookies
- JavaScript Browser BOM
- JavaScript - Browser Object Model
- JavaScript - Window Object
- JavaScript - Document Object
- JavaScript - Screen Object
- JavaScript - History Object
- JavaScript - Navigator Object
- JavaScript - Location Object
- JavaScript - Console Object
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript Events
- JavaScript - Events
- JavaScript - DOM Events
- JavaScript - addEventListener()
- JavaScript - Mouse Events
- JavaScript - Keyboard Events
- JavaScript - Form Events
- JavaScript - Window/Document Events
- JavaScript - Event Delegation
- JavaScript - Event Bubbling
- JavaScript - Event Capturing
- JavaScript - Custom Events
- JavaScript Error Handling
- JavaScript - Error Handling
- JavaScript - try...catch
- JavaScript - Debugging
- JavaScript - Custom Errors
- JavaScript - Extending Errors
- JavaScript Important Keywords
- JavaScript - this Keyword
- JavaScript - void Keyword
- JavaScript - new Keyword
- JavaScript - var Keyword
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM Methods & Properties
- JavaScript - DOM Document
- JavaScript - DOM Elements
- JavaScript - DOM Attributes (Attr)
- JavaScript - DOM Forms
- JavaScript - Changing HTML
- JavaScript - Changing CSS
- JavaScript - DOM Animation
- JavaScript - DOM Navigation
- JavaScript - DOM Collections
- JavaScript - DOM NodeList
- JavaScript - DOM DOMTokenList
- JavaScript Advanced Chapters
- JavaScript - Bubble Sort Algorithm
- JavaScript - Circular Reference Error
- JavaScript - Code Testing with Jest
- JavaScript - CORS Handling
- JavaScript - Data Analysis
- JavaScript - Dead Zone
- JavaScript - Design Patterns
- JavaScript - Engine and Runtime
- JavaScript - Execution Context
- JavaScript - Function Composition
- JavaScript - Immutability
- JavaScript - Kaboom.js
- JavaScript - Lexical Scope
- JavaScript - Local Storage
- JavaScript - Memoization
- JavaScript - Minifying JS
- JavaScript - Mutability vs Immutability
- JavaScript - Package Manager
- JavaScript - Parse S-Expressions
- JavaScript - Prototypal Inheritance
- JavaScript - Reactivity
- JavaScript - Require Function
- JavaScript - Selection API
- JavaScript - Session Storage
- JavaScript - SQL CRUD Operations
- JavaScript - Supercharged Sorts
- JavaScript - Temporal Dead Zone
- JavaScript - Throttling
- JavaScript - TRPC Library
- JavaScript - Truthy and Falsy Values
- JavaScript - Upload Files
- JavaScript - Date Comparison
- JavaScript - Recursion
- JavaScript - Data Structures
- JavaScript - Base64 Encoding
- JavaScript - Callback Function
- JavaScript - Current Date/Time
- JavaScript - Date Validation
- JavaScript - Filter Method
- JavaScript - Generating Colors
- JavaScript - HTTP Requests
- JavaScript - Insertion Sort
- JavaScript - Lazy Loading
- JavaScript - Linked List
- JavaScript - Nested Loop
- JavaScript - Null Checking
- JavaScript - Get Current URL
- JavaScript - Graph Algorithms
- JavaScript - Higher Order Functions
- JavaScript - Empty String Check
- JavaScript - Form Handling
- JavaScript - Functional Programming
- JavaScript - Parameters vs Arguments
- JavaScript - Prototype
- JavaScript - Reactive Programming
- JavaScript - Reduce Method
- JavaScript - Rest Operator
- JavaScript - Short Circuiting
- JavaScript - Undefined Check
- JavaScript - Unit Testing
- JavaScript - Validate URL
- JavaScript Miscellaneous
- JavaScript - Ajax
- JavaScript - Async Iteration
- JavaScript - Atomics Objects
- JavaScript - Rest Parameter
- JavaScript - Page Redirect
- JavaScript - Dialog Boxes
- JavaScript - Page Printing
- JavaScript - Validations
- JavaScript - Animation
- JavaScript - Multimedia
- JavaScript - Image Map
- JavaScript - Browsers
- JavaScript - JSON
- JavaScript - Multiline Strings
- JavaScript - Date Formats
- JavaScript - Get Date Methods
- JavaScript - Set Date Methods
- JavaScript - Modules
- JavaScript - Dynamic Imports
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - Shallow Copy
- JavaScript - Call Stack
- JavaScript - Reference Type
- JavaScript - IndexedDB
- JavaScript - Clickjacking Attack
- JavaScript - Currying
- JavaScript - Graphics
- JavaScript - Canvas
- JavaScript - Debouncing
- JavaScript - Performance
- JavaScript - Style Guide
JavaScript - DOM Navigation
In JavaScript, with HTML DOM we can navigate the tree nodes using the relationship between the nodes. Each HTML element is represented as a node in the DOM tree. The HTML document object is represented as root node. There are different types of nodes such as root node, parent, child and sibling nodes. The relationship between these nodes help to navigate the DOM tree.
What are DOM Nodes?
When a webpage gets loaded in the browser, it creates a document object. The 'document' object is the root of the web page, and you can access other HTML nodes of the web page.
In the HTML DOM, everything is a node.
The 'document' is a parent node of each node.
Each HTML element is a node.
The text inside the HTML element is a node.
All comments inside the HTML are node is the node.
You can access all nodes of the HTML DOM.
Relationship between HTML DOM Nodes
In the HTML DOM, each node has a relationship with other nodes. Each node is present in the hierarchical structure in the DOM tree.
Here are the terms which we will use in this chapter.
Root node − The document node is a root node.
Parent node − Each node has a single parent node. The root node doesn't have any parent node.
Child node − Each node can have multiple and nested childrenders.
Sibling node − The sibling nodes are at the same level, having the same parent node.
Let's understand the relationship between nodes in the below example.
Example
JavaScrip - DOM Navigation Hi Users!
Hello World!
In the above program,
is a root node, and it doesn't have a parent node.
The node contains two child elements:
and .The
element contains the single child element:. The
node contains the single node. The
element contains the single child node:.The
node contains two child nodes:and
.
and
are siblings.
The parent of the
and
is
node.The parent of the
node is node.Navigating Between Nodes Using JavaScript
Navigating between nodes means finding the parent, child, sibling, etc. element of a particular element in the DOM tree.
You can use the below methods and properties to navigate between nodes in the DOM tree.
Property Description firstChild To get the first child of the particular node. It can also return the text, comment, etc. firstElementChild To get the first child element. For example, ,
,, etc.
lastChild To get the last child of the particular node. It can also return the text, comment, etc. lastElementChild To get the last child element. childNodes To get the node list of all children of the particular element. children To get the HTML collection of all children of the particular element. parentNode To get the parent node of the HTML element. parentElement To get the parent element node of the HTML element. nextSibling To get the next node from the same level having the same parent node. nextElementSibling To get the next element node from the same level having the same parent node. previousSibling To get the previous node from the same level having the same parent node. previousElementSibling To get the previous element node from the same level having the same parent node. Below, we will use each method to navigate through the DOM elements.
Accessing the First Child Element
You can access the particular child element using the firstChild or firstElementChild property.
Syntax
Follow the syntax below to use the 'firstChild' and 'firstElementChild' properties to access the first child element.
element.firstChild; element.firstChildElement;
Example
In the below code,
element contains the text followed by threeelements.
When we use the 'firstChild' property, it returns the text node containing the 'Numbers' text, and when we use the 'firstChildElement' property, it returns the first
element.
NumbersOne
Two
Three
Accessing the Last Child Element
You can use the lastChild or lastChildElement properties to access the last child of the particular HTML node.
Syntax
Follow the syntax below to use the 'lastChild' and 'laststElementChild' properties to access the las=st child element.
element.lastChild; element.lastChildElement;
Example
In the below code, we have defined the
element containing 3elements containing the name of the programming languages.
In the output, you can see that the lastElementChild property returns the last
element, and the lastChild property returns the text node of the div element.
Java
JavaScript
HTML
Programming LanguagesAccessing all children of HTML Element
You can use the childNodes property to access the node list of all children elements or the children property to access the HTML collection of all children.
Syntax
Follow the syntax below to access all children elements of the DOM element.
element.children; element.childNodes;
Example
In the below code, we use the childNodes property to access all child nodes of the
element.In the output, you can see that it also returns the text nodes with undefined text as it contains the text nodes after and before each HTML element node.
Java
JavaScript
HTML
programming LanguagesAccessing the Parent Node of HTML Element
You can use the parentNode property to access the parent node of the particular HTML node.
Syntax
Follow the syntax below to use the parentNode property.
element.parentNode;
Example
In the below code, we access the
- element having d equal to 'blue' in JavaScript. After that, we use the parentNode property to access the parent node.
It returns the 'UL' node, which we can observe in the output.
- Blue
- Pink
- Red
The child node of the color list is:Accessing the Next Sibling Node
The nextSibling property is used to access the next sibling.
Syntax
Follow the syntax below to use the nextSibling property.
element.nextSibling
Example
In the below code, we have access to the
- element with an id equal to 'apple', and access to the next sibling node using the nextSibling property. It returns the
- node having the id equal to 'banana'.
- Apple
- Banana
- Watermealon
The next sibling node of the apple node is:Accessing the Previous Sibling Node
The previousSibling property is used to access the previous sibling node from the DOM tree.
Syntax
Follow the syntax below to use the previousSibling property.
element.previousSibling;
Example
In the below code, we access the previous sibling of the
- element with an id equal to 'banana. It returns the
- element having id equal to 'apple'.
- Apple
- Banana
- Watermealon
The previous sibling node of the banana node is:DOM Root Nodes
The HTML DOM contains two root nodes.
document.body − It returns the
element of the document.document.documentElement − It returns the entire HTML document.
Example: Using the document.body
This is demo!Example: Using the document.documentElement
Hi, Users!
DOM nodeName Property
The nodeName property of the HTML DOM element is used to get the name of the node, and it has specifications given below.
It is read−only. You can't modify it.
The value of the nodeName property is equal to the tag name in the upper case.
The node name of the text node is #text.
The node name of the document node is #document.
Syntax
Follow the syntax below to use the nodeName property to get the name of the node.
element.nodeName;
Example
In the below code, we access the
element and use the nodeName property. It returns the tag name in the uppercase.DOM nodeValue Property
The nodeValue is used to get the value of the, and it has specifications given below.
It is also a read-only property.
The node value for the text node is the text itself.
The node value for the element nodes is null.
Syntax
Follow the syntax below to use the nodeValue property to get the value of the node.
element.nodeValue;
Example
The
element contains some text, and theelement in the below code.
The first child element of the
element is the text node, and the second child node of theelement is theelement.
In the output, you can see that when you use the nodeValue property with the text node, it returns the text. Otherwise, it returns null when you use it with the HTML element node.
NumbersOne
Types of Node in DOM
Here, we have given different node types in the HTML DOM.
Node Type Description Element Nodes 1 The element nodes can have child nodes, attributes, and text content. For example, Text Nodes 3 The text nodes can have text content inside the node. For example, text inside the ,
, etc. elements.Comment Nodes 8 The comment nodes contain the comments. Document Nodes 9 It represents the entire document. Document Type Node 10 It represents the type of the document. For example, DOM nodeType Property
The nodeType property returns the type of the node as shown in the above table.
Syntax
Follow the syntax below to use the nodeType property to get the type of the node.
element.nodeType;
Example
In the below code, we use the nodeType property to get the type of the node.
Advertisements