Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Finding an element by ID and accessing the document root and body

Use `getElementById()` to find an element by ID, `documentElement` to get the document’s root element, and `body` to access its HTML body. Each can return `null` for a different reason.

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

Syntax

doc.getElementById(id) // id: string; returns Element or null
 doc.documentElement // read-only; returns Element or null
 doc.body // returns HTMLBodyElement, HTMLFrameSetElement, or null
 doc.body = newBody // assigns an HTML body or frameset element

Document.getElementById/documentElement/body groups one method and two properties. doc.getElementById(id) searches the tree of doc. Pass the exact id value without adding a # prefix: it is not a CSS selector, and case matters. The method returns a matching Element or null. IDs should be unique; if they are duplicated, the first match in tree order wins. The search excludes detached nodes and does not enter shadow trees.

doc.documentElement is read-only: it returns the root element (<html> in an HTML document; XML can have a different root) or null if no root element exists. doc.body returns the first direct child of the <html> element that is either <body> or the legacy <frameset>, or null if there is no such child. Read both properties without parentheses.

Unlike documentElement, body can be assigned to replace the existing element; its children are not moved into the replacement. Assigning null or an HTMLElement that is neither a <body> nor a <frameset> throws HierarchyRequestError. If the element does not implement HTMLElement, as with an SVG element, Web IDL conversion throws a TypeError before the setter runs. Assigning a valid <body> or <frameset> also throws HierarchyRequestError if the document has no root element.

During parsing, the body or target might not exist yet. If you depend on the page’s markup, wait for DOMContentLoaded when document.readyState === "loading"; otherwise run immediately. Check for null before using the result: reading a property directly from null throws a TypeError. The DOM and HTML standards define these APIs, not ECMAScript. The ECMAScript 2024 version label does not indicate support for them; do not assume a JavaScript runtime provides document.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Document lookup demo</title>
</head>
<body>
  <script>
    const doc = document.implementation.createHTMLDocument("Demo");
    const panel = doc.createElement("section");
    panel.id = "Panel";

    console.log(doc.documentElement.localName);
    console.log(doc.body.localName);
    console.log(doc.getElementById("Panel"));

    doc.body.appendChild(panel);
    console.log(doc.getElementById("Panel") === panel);
    console.log(doc.getElementById("panel"));
  </script>
</body>
</html>

Result

html
body
null
true
null

Save the example as an HTML file, open it in a browser, and check the developer console. The separate HTML document already has `html` and `body` elements and does not change the page running the script. `Panel` is found only after insertion, and `panel` does not match because of case.

Related lessons

  • JavaScript · Lesson 37 · Advanced · Version: 4

Sources