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.
const next = item.nextElementSibling; const first = list.firstElementChild; const count = list.children.length;
Try it
Syntax
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
firstChildis often#textand whychildNodes.lengthis larger thanchildren.length. - Live collections.
childrenandchildNodeschange when the DOM changes. Copy them withArray.from()or[...list]before you loop and remove items. - Array methods.
HTMLCollectionhas nomap()orforEach(). Convert it first:[...el.children].map(...). ANodeListfromchildNodeshasforEach()but notmap(). - Document and fragments.
document.parentNodeisnull. The children of aDocumentFragmenthave the fragment asparentNode, butparentElementisnull. - 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 repeatedparentElement.
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.
Related
Element.closest()โ nearest matching ancestorNode.contains()โ descendant testdocument.querySelectorAll()โ search by selector- Lesson: DOM traversal
- Lesson: The DOM