The DOM Explained
When a browser loads an HTML file, it doesn't keep the text as it is. It builds the Document Object Model (DOM): a tree of objects, one for every element and piece of text on the page. JavaScript can read and change those objects, and the browser redraws the page straight away. That is how every interactive website works.
From HTML to a tree
Here is a small page for a mama mboga's price list:
<!DOCTYPE html>
<html>
<head>
<title>Mama Mboga</title>
</head>
<body>
<h1>Today's prices</h1>
<ul id="prices">
<li>Sukuma wiki: KES 20</li>
<li>Tomatoes: KES 10</li>
</ul>
</body>
</html>
The browser reads this from top to bottom and builds a family tree. Every tag becomes an element node, and the words inside tags become text nodes. At the very top sits the document object, which represents the whole page.
li also holds its own text node (not drawn), and the browser fills in anything you leave out, such as a missing head.Parents, children and siblings
Because the DOM is a tree, nodes have family relationships. body is the parent of h1 and ul; those two are siblings; the two li elements are children of the ul. Every element object has properties to move around the tree:
const list = document.getElementById("prices");
console.log(list.tagName); // the element's tag, in capitals
console.log(list.children.length); // element children only
console.log(list.parentElement.tagName); // one level up
console.log(list.firstElementChild.textContent); // text of the first li
console.log(list.previousElementSibling.tagName); // the h1 before it
You will learn many more ways to find elements in Selecting Elements. For now, notice that the DOM gives every part of the page a JavaScript object with properties you can read.
The spaces and line breaks between tags also become text nodes. That is why list.childNodes.length is 5 here (three whitespace text nodes plus two li elements), while list.children.length is 2. Use the "Element" versions (children, firstElementChild, nextElementSibling) to skip the whitespace.
The document object
document is your entry point to the DOM. It is created automatically and is available in every script that runs in a page.
| Property | What it gives you | Value for our page |
|---|---|---|
document.title | The text in <title> (you can also set it) | "Mama Mboga" |
document.documentElement | The <html> element | the root element |
document.head | The <head> element | the head element |
document.body | The <body> element | the body element |
Changing the DOM changes the page. Run this in the console of any page and watch the browser tab's title change immediately:
document.title = "Bei za leo"; console.log(document.title);
The DOM is not your HTML file
Your HTML file is the starting recipe; the DOM is the living result. Three differences matter:
- JavaScript changes the DOM, not the file. Open DevTools (F12) and the Elements panel shows the current DOM, including anything your script added. "View page source" still shows the original file.
- The browser repairs mistakes. If you forget to close a
<p>or leave out<head>, the browser fixes the tree as it builds it. - Changes are lost on reload. Refreshing rebuilds the DOM from the file. To keep data between visits you need storage such as localStorage.
A script in the <head> runs before the browser has built the body, so document.getElementById("prices") returns null and the next line fails with "Cannot read properties of null". Put your <script> just before </body>, or add defer to it, as explained in Adding JavaScript to a Page.
Try it: your first DOM change
This page finds the heading, reports a few facts about it on the page, and changes it when you click the button. Try changing the text, or make the button also change document.title.
<!DOCTYPE html>
<html>
<head>
<title>DOM demo</title>
</head>
<body>
<h1 id="greeting">Habari!</h1>
<p id="facts"></p>
<button id="change">Change heading</button>
<script>
const heading = document.getElementById("greeting");
const facts = document.getElementById("facts");
facts.textContent = "Tag: " + heading.tagName +
", parent: " + heading.parentElement.tagName +
", next sibling: " + heading.nextElementSibling.tagName;
document.getElementById("change").addEventListener("click", () => {
heading.textContent = "Karibu to the DOM!";
});
</script>
</body>
</html>
Key points
- The browser turns HTML into the DOM: a tree of objects with
documentat the top. - Tags become element nodes and text becomes text nodes; nodes have parents, children and siblings.
- Changing a DOM object updates the page instantly, but not your HTML file, and reloading starts again.
- Scripts must run after the elements they use exist: put them at the end of
bodyor usedefer.
Tip: use the ← and → keys to move between pages.
