Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท DOM: Document and Elements

parentElement, children and sibling properties

Reference for DOM tree navigation properties: parentNode, parentElement, children, childNodes, firstElementChild, nextElementSibling and how they differ.

Once you have one element, you can reach its neighbours through properties on the element itself. Going up gives you the parent, going down gives you the children, and going sideways gives you the siblings.

Two families exist. The element properties (children, firstElementChild, nextElementSibling, ...) see only elements and skip text and comments. The node properties (childNodes, firstChild, nextSibling, ...) see everything, including the whitespace text between tags. For everyday work, the element family is almost always what you want.

JavaScript
const next = item.nextElementSibling;
const first = list.firstElementChild;
const count = list.children.length;

Try it

Element and node views
Live collections

Syntax

JavaScript
node.parentNode
node.parentElement
element.children
node.childNodes
element.firstElementChild
element.lastElementChild
element.childElementCount
element.previousElementSibling
element.nextElementSibling
node.firstChild
node.lastChild
node.previousSibling
node.nextSibling
Property Returns
parentNode The parent node (an element, the document, or a document fragment), or null.
parentElement The parent only if it is an element, otherwise null. For <html> it is null.
children A live HTMLCollection of the child elements. Indexable, has length, no forEach().
childNodes A live NodeList of all child nodes, including text and comments. It has forEach().
firstElementChild, lastElementChild The first or last child element, or null if there are none.
childElementCount The number of child elements, the same as children.length.
previousElementSibling, nextElementSibling The adjacent sibling element, or null at the ends.
firstChild, lastChild, previousSibling, nextSibling The same movements, but over all nodes, so whitespace text is often returned.

Usage notes

  • Whitespace is a node. The line break and indentation between two tags form a text node. That is why firstChild is often #text and why childNodes.length is larger than children.length.
  • Live collections. children and childNodes change when the DOM changes. Copy them with Array.from() or [...list] before you loop and remove items.
  • Array methods. HTMLCollection has no map() or forEach(). Convert it first: [...el.children].map(...). A NodeList from childNodes has forEach() but not map().
  • Document and fragments. document.parentNode is null. The children of a DocumentFragment have the fragment as parentNode, but parentElement is null.
  • Shadow DOM. Properties do not cross shadow boundaries: the children of a shadow root are not part of the host's children.
  • Prefer selectors for deep searches. To find something several levels down, use querySelector() instead of chaining many of these properties, which breaks when the markup changes.
  • Going up by selector. closest() is more robust than repeated parentElement.

Browser support

Supported in every browser. The element-only properties on all node types, such as children on document and fragments, are available in all current browsers.