liveBook · Manning

Document Object Model

As explained in Vanilla Web

The browser programming interface for querying and manipulating a document's DOM representation.

The Document Object Model (DOM) is the browser’s live, in-memory tree representing a document. Its nodes include elements, text, and comments, with parent, child, and sibling relationships. JavaScript can query and change those objects to update the interface without a UI library. The DOM is not the HTML source: it begins with parsed HTML and can change at runtime. section 6.0 section 6.2

const title = document.createElement("h1");
title.textContent = "Hello DOM";
document.body.prepend(title);

This small example creates an element, sets plain text, and inserts it at the start of the document body. section 6.2

Use the native API according to the job:

  • Find elements with selectors, then traverse parents, children, or siblings. `children` excludes text and comments; `childNodes` includes them. A collection may be live or a static snapshot, so check before iterating while changing the tree. section 6.2.5 section 6.2
  • Set plain text with `textContent`; create elements for structure. Use `append`, `before`, or `replaceWith` to arrange nodes. Moving an existing node relocates it rather than copying it, preserving its state and listeners. section 6.2 section 6.2.10
  • Respond to UI events with listeners, and change presentation through classes or data attributes while leaving visual rules to CSS. Use `innerHTML` for controlled templates rather than as the default way to insert dynamic content. section 6.4

Render data from a template

For a shopping-cart list, clone an HTML `template` for each item, fill its name, quantity, and price through selected elements’ `textContent`, append the clones to a list, then replace the component’s children with that list. This reuses parsed structure instead of constructing HTML strings for each item. section 8.3

DOM changes happen synchronously, but the browser does not repaint after every property assignment; rendering waits until JavaScript yields. Even so, many uncoordinated edits can add layout and painting work. Build several nodes in a fragment or detached container before attaching them. For components, distinguish the page DOM from a shadow DOM: without a shadow root, component and page styles can affect each other. section 6.1 section 6.2.10 section 7.8.2

How Vanilla Web covers it

How deep it goes
Explains node relationships, lookup, mutation, rendering timing, and practical API choices, without descending into rendering-engine internals.
Examples
Creates a heading with `createElement` and renders shopping-cart items by cloning and filling a template.
What it assumes
Assumes familiarity with HTML and JavaScript while establishing the DOM tree and its interfaces.
What you can do after
Build it from scratch with native DOM APIs to query, create, move, and update interface nodes.
How it connects
Connects DOM operations to events, CSS, templates, custom elements, and shadow DOM.

Also explained in

Compare how 12 books explain Document Object Model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.