The browser
The DOM: the page's tree
The page, turned by the browser into a tree of objects that JavaScript can read and change.
Why it exists
HTML is just text, and you can't tell text to "strike out a menu item" or "add a message to the chat". For a program to change a page while someone's looking at it, the browser needs a live model of the document: objects with properties and methods. The DOM is that model — the common language of HTML, CSS and JavaScript. Without it, every change would mean reloading the page from the server, the way the web worked in the nineties.
Where it's used
- Anything interactive: open a menu, show an error under a form field, strike out a finished task.
- Rendering data from a server: catalog products, chat messages, comments.
- Frameworks — React, Vue, Svelte — end up calling the same DOM methods; they just decide which ones for you.
- UI tests: a test finds a button in the DOM, "clicks" it and checks what changed on the page.
Syntax
document // the whole page
document.body // the <body> element
el.parentElement // the parent
el.children // child elements
el.tagName // "LI", "UL"…Examples
function describe(el) {
return `${el.tagName}: ${el.children.length} children, parent ${el.parentElement.tagName}`;
}
// In a browser: describe(document.querySelector("#menu"))
// returns "UL: 3 children, parent BODY"const page = {
tag: "body",
children: [
{ tag: "h1", children: [] },
{
tag: "ul",
children: [
{ tag: "li", children: [] },
{ tag: "li", children: [] },
],
},
],
};
function countElements(node) {
let total = 1;
for (const child of node.children) total += countElements(child);
return total;
}
countElements(page); // → 5
countElements(page.children[1]); // → 3const menu = {
tag: "ul",
text: "",
children: [
{ tag: "li", text: "Latte", children: [] },
{ tag: "li", text: "Cheesecake", children: [] },
],
};
function textOf(node) {
return node.text + node.children.map(textOf).join("");
}
textOf(menu); // → "LatteCheesecake"textContent: an element's text is the text of all its descendants, back to back.function path(el) {
const parts = [];
for (let node = el; node; node = node.parentElement) {
parts.unshift(node.tagName.toLowerCase());
}
return parts.join(" > ");
}
// In a browser, for a menu item: path(li) returns "html > body > ul > li"parentElement — all the way to <html>.Results after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
The browser reads HTML top to bottom and builds a tree of nodes. There are several kinds: elements (tags), text nodes (the text between tags, including line breaks and spaces), comments. That's why there are two kinds of "children": children holds only elements, childNodes holds every node, text included.
Every element is a JavaScript object linked to its neighbours: parentElement, children, nextElementSibling. The tree has one root, document; under it is <html>, which holds <head> and <body>.
When you change the tree (text, classes, new elements), the browser marks the page as changed and redraws it — not after every line, but once the script finishes. So ten changes in a row don't cause ten "flickers".
The DOM isn't part of the JavaScript language — it's a browser API. Node.js has no document, which is why UI tests plug in an emulation of it (jsdom, happy-dom).
Traps
When to pick something else
When the interface is big and the data changes often, manual DOM edits quickly turn into a mess of "what's already drawn where". That's when people reach for a framework (React, Vue, Svelte): you describe how the page looks for given data, and it works out the smallest DOM changes itself. And don't keep data in the DOM: arrays and objects are the source of truth, and the page is their reflection.
Practice
Lessons where this appears
- A website for the Purrfect CaféTale · Chapter 18 · DOM: the page as a tree
- Cats looking for a homeLab · Chapter 18 · DOM: the page as a tree
- The shop window that purrsTale · Chapter 19 · Events
- Why does a click bubble?Why? · Chapter 19 · Events
- The toy shop windowWorkshop · Chapter 19 · Events
- The Red DotBoss · Chapter 19 · Events