Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

DOM document: ID lookup, root element, and title

Find an element by its exact ID, access the document’s root element, and read or change its title. Distinguish a missing node from an empty string.

Version: Current web platform; runtime support varies

Syntax

document.getElementById(id)
document.documentElement
document.title
document.title = value

Document.getElementById/documentElement/title groups one method and two properties. document.getElementById(id) looks for an exact, case-sensitive id in that document’s tree. Pass the ID itself without adding #: the argument is not a CSS selector. The method returns an Element or null; check before accessing element properties. IDs should be unique within the tree. If duplicates exist, the method returns the first match in tree order.

The lookup uses the tree as it exists at the time of the call. It misses elements not yet inserted into the document and elements in a separate shadow tree. document.documentElement is read-only: it gives you the root element, normally html in an HTML document, or null if there is no root element. It is not document.body.

document.title is a readable and writable string property, not the title node, an element’s title attribute, or a visible heading. In a typical HTML document, reading it trims leading and trailing ASCII whitespace and collapses internal runs to single spaces. With no title element it returns "". Assigning to it replaces the title contents with text, not HTML markup. If title is missing, assignment creates it in head if present; otherwise it does nothing. For a root svg in the SVG namespace, assignment updates or creates a direct SVG title child, which is where the getter reads the title.

The DOM and HTML standards define these web platform APIs. Using them does not require ECMAScript 2024. For a document displayed in a browser tab, the title normally appears in the tab label. An environment without a DOM need not provide document.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>DOM document example</title>
</head>
<body>
  <script>
    {
      const doc = document.implementation.createHTMLDocument("Draft");
      const item = doc.createElement("p");
      item.id = "status";

      console.log(doc.title);
      console.log(doc.getElementById("status") === null);
      doc.body.append(item);
      console.log(doc.getElementById("status") === item);
      console.log(doc.getElementById("Status") === null);
      console.log(doc.documentElement.localName);

      doc.title = "  Ready   to publish  ";
      console.log(doc.title);
    }
  </script>
</body>
</html>

Result

Draft
true
true
true
html
Ready to publish

Save the whole example as an HTML file, open it in a browser, and check the developer-tools console. It creates a separate HTML document, so it does not change the current page or tab title. The three `true` values show, in order, no match before insertion, object identity after insertion, and case-sensitive matching.

Related lessons

  • JavaScript · Lesson 38 · Advanced · Version: 4

Sources