TExcelTAD
Home › JavaScript › DOM Intro

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:

html
<!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:

html · try it yourself
<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:

html · try it yourself
<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:

html · try it yourself
<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?

  1. Data Object Model
  2. Document Object Model
  3. Dynamic Output Mode
  4. Document Order Map

When JavaScript changes the DOM, what happens to the original .html file?

  1. It is rewritten
  2. Nothing — only the in-memory tree changes
  3. It is downloaded again
  4. It is deleted

In <p>Hi <b>there</b></p>, what is the <b> element to the <p>?

  1. Its parent
  2. Its sibling
  3. Its child
  4. Its root