JavaScript DOM Navigation lets you move through HTML elements in the document tree. Using properties like parentNode, children, and nextElementSibling, you can reach parent, child, and sibling elements to read or update the page dynamically.
parentNode, childNodes, firstChild, lastChild, nextSibling, previousSibling.parentElement, children, firstElementChild, lastElementChild, nextElementSibling, previousElementSibling.<div>, <li>.for...of or loops to visit each child element of a parent.document.body.children[0] to move deeper into the DOM tree.Node-based properties work with all node types (elements, text, comments, etc.):
parentNode – returns the parent node.childNodes – returns a NodeList of all child nodes (including text nodes and whitespace).firstChild, lastChild – return the first and last child nodes.nextSibling, previousSibling – move to the next or previous sibling node.Element-based properties ignore text nodes and work only with HTML elements:
parentElement – returns the parent element node.children – returns an HTMLCollection of element children only.firstElementChild, lastElementChild – first/last child that is an element.nextElementSibling, previousElementSibling – next/previous sibling that is an element.Both give you the parent, but there is a subtle difference:
parentNode can return any parent node (including document).parentElement always returns an element node (or null if the parent is not an element).You can loop through children to access or modify each element:
for...of with children to loop through element children.childNodes, you may need to check nodeType === 1 to filter element nodes.You can chain navigation properties to move deeper into the DOM: document.body.children[0].children[1] moves from the body to its first child and then to that element’s second child.
<div id="box">
<p>Hello</p>
<span>World</span>
</div>
<script>
const box = document.getElementById("box");
console.log(box.firstChild); // might return a text node
console.log(box.childNodes); // includes text nodes
</script>
<ul id="list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<script>
const list = document.getElementById("list");
const secondItem = list.children[1];
console.log(secondItem.innerText); // Item 2
console.log(secondItem.nextElementSibling.innerText); // Item 3
console.log(secondItem.previousElementSibling.innerText); // Item 1
</script>
<div id="child">Text</div>
<script>
const child = document.getElementById("child");
console.log(child.parentNode); // Can be element or document
console.log(child.parentElement); // Always returns an element
</script>
<ul id="menu">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
<script>
const menu = document.getElementById("menu");
for (let item of menu.children) {
console.log(item.innerText);
}
</script>
<script>
const target = document.body.children[0].children[1];
console.log(target);
</script>
box.firstChild may return a text node (whitespace before the first element), and box.childNodes shows all nodes, including text and elements.list.children[1] selects the second <li>, and nextElementSibling / previousElementSibling move to “Item 3” and “Item 1”.parentNode can be the document or another node, while parentElement guarantees an element (or null).for...of loop prints the text of each menu item: “Home”, “About”, “Contact”.document.body.| Property | Returns |
|---|---|
parentNode |
Parent node (may be element or document) |
parentElement |
Only the parent element |
children |
HTMLCollection of element children |
childNodes |
NodeList of all child nodes |
firstChild / lastChild |
First/last node (can be text) |
firstElementChild / lastElementChild |
First/last element only |
nextSibling / previousSibling |
Next/previous node |
nextElementSibling / previousElementSibling |
Next/previous element |
children over childNodes when you only care about HTML elements.null before accessing nextElementSibling or previousElementSibling.for...of with children to loop through element children cleanly.childNodes, filter with nodeType === 1 to get only elements.childNodes.children.<div> using children[1].nextElementSibling to highlight the next item in a list.childNodes and log only those with nodeType === 1.lastElementChild and change its background color.