DOM Introduction
When a browser loads a web page, it reads the HTML and builds a tree of objects in memory called the DOM — the Document Object Model. JavaScript doesn't change your .html file; it changes this live tree, and the browser immediately redraws the page to match. Every interactive thing you've seen on a website — menus opening, likes counting up, items added to a cart — is JavaScript reading and modifying the DOM.
The page as a tree
Take this small document:
<!doctype html>
<html>
<head>
<title>My page</title>
</head>
<body>
<h1>Welcome</h1>
<p>Hello <b>world</b></p>
</body>
</html>The browser turns it into a tree of nodes:
document
└── html
├── head
│ └── title
│ └── "My page" (text node)
└── body
├── h1
│ └── "Welcome"
└── p
├── "Hello "
└── b
└── "world"| Term | Meaning |
|---|---|
| node | any item in the tree: the document, elements, text, comments |
| element | a node created from an HTML tag, like <p> |
| parent / child | body is the parent of h1; h1 is a child of body |
| siblings | nodes with the same parent (h1 and p) |
| root | the document object at the top |
The document object
document is your entry point to the DOM. It has properties for important parts of the page and methods for finding elements:
<h1 id="title">Hello DOM</h1>
<p class="intro">The DOM is a tree of objects.</p>
<p class="intro">JavaScript can read and change it.</p>
<script>
console.log("title:", document.title || "(no <title> set)");
console.log("body children:", document.body.children.length);
const heading = document.getElementById("title");
console.log("heading text:", heading.textContent);
console.log("tag name:", heading.tagName);
const intros = document.querySelectorAll(".intro");
console.log("intro paragraphs:", intros.length);
</script>Reading and changing the page
Elements are objects with properties you can read and write. Change a property, and the page updates instantly:
<h2 id="headline">Original headline</h2>
<p id="info">Click the buttons to change the page.</p>
<button id="text">Change text</button>
<button id="color">Change colour</button>
<script>
const headline = document.getElementById("headline");
document.getElementById("text").addEventListener("click", () => {
headline.textContent = "Changed by JavaScript!";
});
document.getElementById("color").addEventListener("click", () => {
headline.style.color = headline.style.color === "crimson" ? "" : "crimson";
});
</script>The DOM is live
The DOM reflects the current state of the page, not the original HTML. If you add an element with JavaScript, it becomes part of the tree just like elements written in HTML:
<ul id="list">
<li>Written in HTML</li>
</ul>
<button id="add">Add item</button>
<p id="count"></p>
<script>
const list = document.getElementById("list");
const count = document.getElementById("count");
const update = () => (count.textContent = `The list has ${list.children.length} item(s).`);
document.getElementById("add").addEventListener("click", () => {
const li = document.createElement("li");
li.textContent = "Added by JavaScript";
list.append(li);
update();
});
update();
</script>Tip: Open your browser's developer tools (F12) and look at the Elements panel: that's the live DOM. Right-click any element on a page and choose Inspect to jump to its node.
What the rest of this section covers
| Chapter | You'll learn to… |
|---|---|
| Selecting Elements | find elements with getElementById, querySelector and friends |
| Changing HTML & CSS | update text, HTML, attributes, classes and styles |
| DOM Events | understand the event object, bubbling and default actions |
| Event Listeners | add/remove listeners and use event delegation |
| Creating & Removing Nodes | build new elements and remove old ones |
| Forms & Validation | read form values and validate user input |
Note: The DOM is a browser feature, not part of the JavaScript language itself. That's why the examples in this section run in a live HTML frame rather than in a plain JavaScript playground, and why Node.js has no
document.
Test yourself
What does DOM stand for?
- Data Object Model
- Document Object Model
- Dynamic Output Mode
- Document Order Map
When JavaScript changes the DOM, what happens to the original .html file?
- It is rewritten
- Nothing — only the in-memory tree changes
- It is downloaded again
- It is deleted
In <p>Hi <b>there</b></p>, what is the <b> element to the <p>?
- Its parent
- Its sibling
- Its child
- Its root