JavaScript

JavaScript is a high-level, interpreted programming language that is widely used for web development. Initially designed as a client-side scripting language, it runs directly in web browsers, enabling dynamic and interactive user experiences. JavaScript can now be used for server-side development as well.

JavaScript HTML DOM Navigation

14 April 2025 | Category:

The HTML DOM (Document Object Model) represents the structure of your web page as a tree of nodes. JavaScript allows you to navigate, access, and manipulate these nodes easily.

DOM navigation helps you move through:

  • Parent elements
  • Children elements
  • Sibling elements

📘 Understanding the DOM Tree

<body>
  <div id="container">
    <p>Hello <span>World</span></p>
  </div>
</body>

This is represented in a tree-like structure:

body
 └── div (id="container")
      └── p
           ├── Text: "Hello "
           └── span
                └── Text: "World"

Using JavaScript, you can navigate through this tree to access any node.


🔍 Accessing DOM Nodes

Here are the most common DOM navigation properties:

PropertyDescription
parentNodeReturns the parent node
childNodesReturns all child nodes (including text)
childrenReturns only element child nodes
firstChildReturns the first child node
firstElementChildReturns the first child that is an element
lastChildReturns the last child node
lastElementChildReturns the last child element
nextSiblingReturns the next node in the same level
nextElementSiblingReturns the next sibling element
previousSiblingReturns the previous sibling node
previousElementSiblingReturns the previous sibling element

đŸ§Ș Example: Navigating the DOM

<div id="box">
  <p>Hello <span>World</span></p>
</div>

<script>
  const box = document.getElementById("box");
  
  // Parent Node
  console.log(box.parentNode); // Might log <body> or another wrapper

  // First child node (could be text, comment, or element)
  console.log(box.firstChild); 

  // First element child
  console.log(box.firstElementChild); // <p>Hello <span>World</span></p>

  // Access <span> through DOM navigation
  const p = box.firstElementChild;
  const span = p.children[0];
  console.log(span.textContent); // World
</script>

📂 Difference Between childNodes vs children

<ul id="myList">
  <li>One</li>
  <li>Two</li>
</ul>
const list = document.getElementById("myList");

console.log(list.childNodes); // NodeList (includes text nodes)
console.log(list.children);   // HTMLCollection (only <li> elements)

✅ Use children if you only want element nodes.
⚠ childNodes includes text nodes (like spaces and line breaks).


🧬 Navigating Siblings

<div>
  <h2>Title</h2>
  <p>This is a paragraph.</p>
</div>

<script>
  const title = document.querySelector("h2");

  // Next element sibling
  console.log(title.nextElementSibling); // <p>This is a paragraph.</p>

  // Previous element sibling (if available)
  console.log(title.previousElementSibling); // null (no previous sibling)
</script>

📌 Practical DOM Navigation Example

Let’s say you want to change the color of the first child element inside a section:

<section id="content">
  <h3>Heading</h3>
  <p>Paragraph text</p>
</section>

<script>
  const section = document.getElementById("content");

  // Change color of first element child
  section.firstElementChild.style.color = "red";
</script>

đŸ§Œ Pro Tips

  • Always check for null when navigating the DOM, especially with siblings or parents.
  • Avoid childNodes if you’re only interested in elements.
  • Use querySelector() or getElementById() for direct access when possible.

✅ Summary

TaskMethod
Get parentelement.parentNode
Get childrenelement.children
Get first child elementelement.firstElementChild
Get last child elementelement.lastElementChild
Get next siblingelement.nextElementSibling
Get previous siblingelement.previousElementSibling