cover-img

JavaScript DOM Traversal Methods

23 July, 2022

11

11

0

Que- What is Traversing the DOM?

Ans- Everything in an HTML DOM is a node; the entire document, every HTML element, text inside HTML elements, attributes, and comments everything is a node; & navigating between these nodes is known as DOM Traversing.

DOM Relationships

The terms parent, child, and sibling are used to describe relationships.

• In a node tree, the top node is called the root
• Every node has exactly one parent, except the root
• A node can have a number of children
• Siblings are nodes with the same parent
img
Syntax:
element.parentElement.nodeName
Methods:
• parentNode & parentElement
• childNodes & children
• firstChild & firstElement
• lastChild & lastElement
• nextSibling & nextElementSibling
• previousSibling & previousElementSibling
1. parentNode & parentElement
These properties return the parent of the specified element. The difference is parentElement returns null if a node does not have any parent.
img
• childNodes & children
These properties return child nodes. The difference is that childNodes returns all child nodes including text, spaces, and comments. Where children only return child elements (not text or comments)
img
• firstChild & firstElement
firstChild is based on childNodes it returns the first node of the element according to childNodes indexing.
firstElementChild is based on children it returns the first node of the element according to children indexing.
img
• lastChild & lastElement
lastChild is based on childNodes it returns the last node of the element according to childNodes indexing.
lastElementChild is based on children it returns the last node of the element according to children indexing.
img
• nextSibling & nextElementSibling
These properties return next (after) siblings of an Element. nextSibling follows the childNodes indexing rule. nextElementSibling follows the children indexing rule.
img
• previousSibling & previousElementSibling
These properties return previous (before) siblings of an Element. previousSibling follows the childNodes indexing rule. previousElementSibling follows the children indexing rule.


img
If you liked this article, make sure to

1.

Follow me ✅

2.

Like this post 👍
Thanks so much for getting to the end of it 🙏😍
Alfaiz ⚡@imAlfaiz
• Aspiring Frontend Developer - Student - Web Dev 🎨
• DM for Collaboration💌

javascript

web development

frontend

dom

11

11

0

javascript

web development

frontend

dom

Alfaiz ⚡
Hii 👋 • Front-end Enthusiast 🎨 • Sharing threads related to web dev on Twitter

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2025. Showcase Creators Inc. All rights reserved.