Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

`contains()` and `parentElement`: DOM descendants and immediate parents

`contains()` checks whether a node is the same node or a descendant. `parentElement` returns the immediate parent only if that parent is an element.

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

Syntax

container.contains(otherNode) // boolean; pass a Node or null
node.parentElement            // Element or null; read-only

In the Node.contains/parentElement pair, container.contains(otherNode) answers a yes-or-no question: it returns true if otherNode is container itself or a descendant at any depth, including a text node. Passing null returns false. Omitting the required argument or passing a string throws a TypeError. To exclude the container itself, use otherNode !== container && container.contains(otherNode).

node.parentElement is a read-only property, not a method. It returns the immediate parent if that parent is an Element. It returns null if there is no parent or the parent is another type, such as a Document or DocumentFragment. It does not look farther up the tree. Use node.parentNode to get the immediate parent regardless of its type.

Both reads reflect the current tree structure, even in a tree detached from the document. Moving a node can change the result. Neither crosses from a shadow tree to its host: the host does not contain its shadow nodes according to contains(), and a direct child of a ShadowRoot has a parentElement of null.

These are members of the DOM Node interface, not ECMAScript 2024 language features. Availability depends on whether the runtime provides the relevant DOM APIs.

Examples

<!doctype html>
<meta charset="utf-8">
<title>Node.contains() and Node.parentElement</title>
<script>
  const fragment = document.createDocumentFragment();
  const section = document.createElement("section");
  const span = document.createElement("span");
  const text = document.createTextNode("Hello");

  fragment.appendChild(section);
  section.appendChild(span);
  span.appendChild(text);

  console.log(`section.contains(text): ${section.contains(text)}`);
  console.log(`section.contains(section): ${section.contains(section)}`);
  console.log(`section.contains(null): ${section.contains(null)}`);
  console.log(`text.parentElement === span: ${text.parentElement === span}`);
  console.log(`section.parentElement: ${section.parentElement}`);
  console.log(`section.parentNode === fragment: ${section.parentNode === fragment}`);
</script>

Result

section.contains(text): true
section.contains(section): true
section.contains(null): false
text.parentElement === span: true
section.parentElement: null
section.parentNode === fragment: true

Save the example as an `.html` file, open it in a browser, and check the output in the developer console. The detached fragment holds `section` but is not an `Element`, so `section.parentElement` is `null` even though `section.parentNode` is the fragment.

Related lessons

  • JavaScript · Lesson 18 · Advanced · Version: 4

Sources