Frontend / The DOM

The DOM

A seven-line HTML file becomes a tree of nodes in the browser. We build that tree, name its parts, find a node with JavaScript and add a new one.

Beginner7 steps~6 min
Element — a node made from a tag pair
Text node — the words inside an element
Attribute — a named value pinned to an element
Script — JavaScript that reads and edits the tree
// THE SHORT VERSION

In 30 seconds

A browser does not work with HTML text directly. It reads the file once and builds a tree of objects from it: the DOM, short for Document Object Model.

Each element is a node. Text sits in its own nodes below the elements, and attributes are values attached to them. Nodes are linked as parents, children and siblings.

JavaScript can find any node and change it, and the page redraws to match. The original file stays as it was, so the DOM and the HTML source can differ.

The DOM, step by step

STEPS01 / 07

Simplified: the real tree starts at document and html, includes head, and keeps the line breaks between tags as extra text nodes. They are left out to keep the drawing small.

STEP 01 / 07 A page starts as text
THE MATH
index.html = 7 lines of text
  1. Step 1: A page starts as text. The server sends the browser a file of plain text: tags, words, nothing more. The browser cannot draw text like this directly, and a script cannot easily change it. So before anything appears, the browser turns the text into a structure it can work with.
  2. Step 2: The browser builds a tree. Reading the file top to bottom, the browser makes one object, called a node, for each element. A tag written inside another becomes a branch below it. This tree of nodes, held in memory, is the Document Object Model: the DOM.
  3. Step 3: Three kinds of thing. Elements are the boxes. The words between the tags are nodes too: each run of text hangs below its element as a text node. Attributes such as id are not branches; they are named values attached to their element.
  4. Step 4: Parent, child, sibling. Nodes are described like a family. The node one level up is the parent; the nodes one level down are children; nodes sharing a parent are siblings. Seen from ul: body is its parent, the two li are its children, and h1 is its sibling. Every node carries links to these relatives.
  5. Step 5: Find a node. JavaScript reaches the tree through the document object. querySelector takes a CSS selector and returns the first element that matches it; #list means “the element whose id is list”. querySelectorAll returns every match: for li, both items.
  6. Step 6: Change the tree. A script can make a new node, fill it, and attach it. createElement builds a loose li; setting textContent gives it a text node; append hangs it under the ul as the last child. The browser redraws the page from the tree, so Bread shows up at once.
  7. Step 7: The DOM is not the source. The file on the server never changed; only the tree in memory did. “View source” shows the original text, while the inspector in developer tools shows the live DOM. Reload the page and the browser builds a fresh tree from the file: Bread is gone unless the script runs again.
VALUES SO FAR
source7 lines of textinput
elements····nodes
text····nodes
attr····on ul
parent····of ul
list····found
li count····live
file····unchanged
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

Spotted a mistake on this page?