Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

`document.getElementById()` versus `document.body`

Use `getElementById()` to find an element by its ID, and `document.body` to access the document’s body element whether or not it has an ID.

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

Syntax

document.getElementById(id)
document.body
document.body = newBody

Document.getElementById/body pairs a lookup method with a structural property. document.getElementById(id) searches that document’s tree and returns an Element or null. Pass the exact id value, such as "note", not a CSS selector such as "#note". Matching is case-sensitive. IDs should be unique; if duplicated, the first match in tree order wins. Elements not yet inserted into the document tree are not found.

Read document.body without parentheses. It returns the first direct child of the root HTML html element that is an HTML body or a legacy HTML frameset, or null if there is none. document.getElementById("body") looks for the ID body, not the tag; it can return a different element or null even when document.body exists.

Assigning to document.body can replace the entire existing body node and its subtree. If there is no existing body, the new element is appended to the document element. Assigning null or an element other than an HTML body or frameset, or assigning when there is no document element, throws a DOMException named HierarchyRequestError.

While the page is being parsed, null can mean that the node has not been created yet. For static markup, access it after parsing, for example in a DOMContentLoaded handler registered before the event, and still check the result before using it. These are web platform APIs, not part of ECMAScript 2024; a standard web worker cannot access the page’s document.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>DOM lookup example</title>
</head>
<body>
  <script>
    (() => {
      const doc = document.implementation.createHTMLDocument("DOM demo");
      const body = doc.body;
      console.log(`Before setting an ID: ${doc.getElementById("body")}`);

      body.id = "body";
      console.log(`Same node: ${doc.getElementById("body") === body}`);
      console.log(`Wrong case: ${doc.getElementById("Body")}`);

      const item = doc.createElement("p");
      item.id = "note";
      console.log(`Before insertion: ${doc.getElementById("note")}`);
      body.appendChild(item);
      console.log(`After insertion: ${doc.getElementById("note") === item}`);
    })();
  </script>
</body>
</html>

Result

Before setting an ID: null
Same node: true
Wrong case: null
Before insertion: null
After insertion: true

Save the example as an `.html` file, open it in a browser, and check the messages in the developer console. The separate document already has a `body` element, but it has no ID. After assigning its ID, both access forms return the same node. The `p` element is found only after insertion.

Related lessons

  • JavaScript · Lesson 6 · Advanced · Version: 4

Sources