Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

DOM element traversal: children, tag name, parent, and siblings

Read an element's tag name and move among direct children, a parent, and neighboring elements. Learn when you get a live collection and when a property can return `null`.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

element.children                // HTMLCollection
element.tagName                 // string
element.parentElement           // Element or null
element.previousElementSibling  // Element or null
element.nextElementSibling      // Element or null

Element.children/tagName/parentElement/previousElementSibling/nextElementSibling groups five read-only DOM properties. Read them with dot notation, without call parentheses. They belong to the DOM API, not ECMAScript 2024; a runtime without a DOM does not provide them.

element.children is a live HTMLCollection of direct child elements in DOM order. It is empty if there are no element children. element.childNodes is also live, but it is a NodeList of all direct child nodes, including text and comments. A saved children collection updates as the DOM changes. Array.from(element.children) captures the current membership and order in an array of element references; it does not clone the elements.

element.tagName returns the qualified tag name as a string, including a prefix if present. For an element in the HTML namespace in an HTML document, ASCII letters are uppercased, as in "DIV". Otherwise the case is preserved, including for SVG in an HTML document and for XHTML processed as XML. For namespace-aware comparisons, use localName together with namespaceURI.

element.parentElement comes from Node. It returns the immediate parent if that parent is an element, or null if there is no parent or the parent is not an element. For example, on a document's root element, parentElement is null but parentNode returns the Document.

previousElementSibling and nextElementSibling return the nearest element before or after the current one, respectively, under the same parent, or null if there is no such element. They skip text and comments, unlike previousSibling and nextSibling, which can return those nodes. Check for null before reading tagName, or use element.nextElementSibling?.tagName, which gives undefined if there is no next element sibling.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>DOM element traversal</title>
</head>
<body>
<script>
{
  const doc = document.implementation.createHTMLDocument();
  const box = doc.createElement('div');
  const first = doc.createElement('span');
  const second = doc.createElement('em');
  box.append(first, ' ', doc.createComment('gap'), second);

  const live = box.children;
  const snapshot = Array.from(live);
  console.log(JSON.stringify([live.length, box.childNodes.length, live[0].tagName]));
  console.log(JSON.stringify([
    first.parentElement?.tagName,
    first.nextElementSibling?.tagName,
    second.previousElementSibling?.tagName,
    first.previousElementSibling,
    second.nextElementSibling
  ]));

  box.append(doc.createElement('strong'));
  console.log(JSON.stringify([
    live.length, snapshot.length,
    doc.documentElement.parentElement,
    doc.documentElement.parentNode === doc
  ]));
}
</script>
</body>
</html>

Result

[2,4,"SPAN"]
["DIV","EM","SPAN",null,null]
[3,2,null,true]

Save the example as an `.html` file, open it in a browser, and check the developer console. It uses a separate HTML document and leaves the open page unchanged. The space and comment count in `childNodes` but not in `children`; appending an element increases only the length of the live collection.

Related lessons

  • JavaScript · Lesson 34 · Intermediate · Version: 3

Sources