LOGBOOK

HELP

1 / 29
Other keys: showSpace: good1-4: rate0: skip5: flag

Question

What is the DOM (Document Object Model)?

Answer

The DOM is a live, tree-structured representation of an HTML document that the browser builds so JavaScript can read and modify page content.

When a browser loads HTML, it parses it into a hierarchical tree structure where:

  • Each HTML element becomes a node in the tree
  • The tree starts with the document object as the root
  • Parent-child relationships mirror HTML nesting

Key characteristics:

  • Live representation - Changes to the DOM immediately reflect on the page
  • Language-neutral interface - Though typically used with JavaScript
  • Standardized by W3C - Consistent across browsers

Example structure:

document
└── html
    ├── head
    │   └── title
    └── body
        ├── h1
        └── p

Tip: Think of the DOM as the "living" version of your HTML that JavaScript can read and modify.

Go deeper:

Illustration
‍Birger Eriksson · CC BY-SA 3.0 · Wikimedia Commons
or press any other key

Question

How do you select a single element by its ID in JavaScript?

Answer

Call document.getElementById("id") (no #), which returns the matching element or null if none exists.

const element = document.getElementById("myButton");

Key characteristics:

  • Returns a single element (IDs must be unique)
  • Returns null if no element matches
  • Fastest selection method (IDs are indexed)
  • Does NOT include the # symbol (unlike CSS)

Common mistake:

// Wrong - don't use #
document.getElementById("#myButton");

// Correct
document.getElementById("myButton");
or press any other key