liveBook · Manning

Chapter 6: The document object model API

What you will learn in chapter 6 of Vanilla Web

Introduces the DOM as the browser’s in-memory document tree and explains why native APIs and careful updates are enough to build interfaces without a library.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces the DOM as the browser’s in-memory document tree and explains why native APIs and careful updates are enough to build interfaces without a library.

Key terms: DOM, tree of nodes, parent, children, siblings, selectors, node constructors, insertion and removal helpers, event system, layout

6.1 The DOM tree

The section explains how document elements become DOM objects that JavaScript can access and change, and why DOM changes do not repaint immediately.

Key terms: DOM, HTMLElement interface, properties, methods, synchronously, main thread, repaint, reference, querying the document, inject it into the DOM

6.2 The API

This section introduces the DOM’s core node types, explains how JavaScript changes reach the screen, and gives practical guidance for handling collections and choosing APIs.

Key terms: Document, Element, Text, parentNode, childNodes, parsed HTML, DOM tree, mutations, layout, paint

6.2.1 HTMLElement interface

Introduces the shared HTMLElement interface, specialized element APIs, and preferred ways to manage classes, data attributes, and styles.

Key terms: HTMLElement, HTMLInputElement, HTMLAnchorElement, classList, dataset, data-* attributes, style, CSS classes

6.2.2 Global DOM objects

This section identifies the main objects through which a page accesses the DOM API and distinguishes the DOM from the broader global environment.

Key terms: window, document, browsing context, nodes, globalThis, DOM, global object

6.2.3 Working with different documents

This section explains how separate DOM documents, same-origin restrictions, off-DOM construction, and cross-document node ownership affect working with nodes.

Key terms: document, node tree, same-origin policy, postMessage, off-DOM construction, DocumentFragment, template element, DOMParser, detached document, adoptNode()

6.2.4 Querying the document

Explains what DOM queries return, how live collections behave, and how to choose and scope selectors.

Key terms: Element, null, HTMLCollection, NodeList, live, static, live collection, Array.from, spread operator, getElementById

6.2.5 Browsing the tree

This section explains how to move through the DOM tree from an element and reminds readers that the tree contains more than HTML elements.

Key terms: DOM tree, HTMLElement, parents, children, siblings, text nodes

6.2.6 Elements available

This section explains that HTML elements have built-in APIs and why code should use those capabilities while checking what each element supports.

Key terms: interface, native element, element’s own API, accessibility, keyboard behavior, semantics, type checks, feature checks, heterogeneous nodes, instanceof

6.2.7 Modifying elements

Explains ways to modify an element and how to choose safer methods for text, dynamic content, and visual states.

Key terms: innerHTML, textContent, XSS, DOMPurify, append, prepend, before, after, replaceWith, property values

6.2.8 Properties vs. attributes

This section distinguishes DOM attributes from JavaScript properties, explains when they stay in sync, and briefly contrasts direct DOM updates with Virtual DOM updates.

Key terms: attributes, properties, HTMLElement, reflected attribute, disabled, open, non-reflected attribute, getAttribute, setAttribute, data-*

6.2.9 Creating elements

This subsection recommends safe methods for creating HTML UI and an in-memory buffer for inserting many nodes.

Key terms: createElement, createTextNode, DocumentFragment, in-memory buffer, intermediate reflows

6.2.10 Element arrangement

This section explains how moving existing DOM nodes differs from recreating them and recommends batching multi-node rearrangements for performance.

Key terms: node, state, event listeners, fragment, detached container, layout recalculations, perceived performance

6.2.11 Custom properties with dataset

This section explains how to use data attributes through dataset for small element metadata and where not to use them.

Key terms: data attributes, dataset, kebab-case, camelCase, behavior hooks, IDs, flags, tiny configurations, in-memory structures

6.2.12 Working with styles

This section explains how to choose between classes, inline styles, CSS custom properties, and CSSOM when working with styles from JavaScript.

Key terms: classes, data attributes, cascade, media queries, inline style, getComputedStyle, CSS custom properties, variables, :root, var()

6.2.13 Working with events

This section explains how to handle DOM events with listeners, propagation, delegation, and custom events.

Key terms: event, event listener, default browser action, preventDefault, on* properties, addEventListener, removeEventListener, passive, once, capture phase

6.3 Browser Web APIs

This section places the DOM among the browser’s built-in Web APIs and explains two checks before using them: secure-context requirements and browser compatibility.

Key terms: DOM, Web APIs, navigator, window, document, secure context, HTTPS, localhost, secure origin, Baseline

6.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 5: Vanilla JavaScript Chapter 7: Web components →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.