Parent & Child Navigation in JavaScript

From the DOM Manipulation cheat sheet · DOM Traversal · verified Jul 2026

Parent & Child Navigation

Navigate between parent and child elements in the DOM tree

javascript
// Parent access
const parent = element.parentElement
const parentNode = element.parentNode

// Children access
const children = element.children  // HTMLCollection
const childNodes = element.childNodes  // NodeList (includes text)

// First and last child
const first = element.firstElementChild
const last = element.lastElementChild

// Check for children
if (element.hasChildNodes()) {
  // Has child nodes
}
💡 children returns only elements, childNodes includes text
📌 closest() is great for event delegation
⚡ parentElement is null for document
✅ Use contains() to check relationships

More JavaScript tasks

Back to the full DOM Manipulation cheat sheet