Tuple Logo
document-object-model-dom

SHARE

Document Object Model (DOM)

What is the Document Object Model?

The Document Object Model (DOM) is a programmable representation of a web page’s structure. When a browser loads an HTML or XML file, it transforms that file into a tree structure known as the DOM. This structure allows programming languages like JavaScript to access and manipulate different parts of the page.

In the DOM, each part of a web page such as headings, paragraphs, links, buttons, and images, is represented as a node in the tree. The entire page is seen as a hierarchy of nested elements, starting from the <html> element as the root of the tree.

Relationship between HTML and the DOM

While HTML is the original source code of a web page, the DOM is the version that the browser reads, interprets, and uses during runtime. This DOM structure can be changed dynamically, even after the page has fully loaded, using JavaScript. This forms the foundation of interactive websites.

For example:

<!-- HTML code -->
<p id="example">Hello world!</p>

 This text can be changed dynamically using JavaScript:

document.getElementById("example").textContent = "Text changed via DOM";

In this example, the original HTML isn’t changed, but the DOM version of it (in the browser’s memory) is.

Why the DOM is important

The Document Object Model is crucial in modern web development because it serves as the bridge between static HTML and dynamic behavior on a website. Thanks to the DOM, developers can:

In short: the DOM allows developers to make websites interactive and responsive, leading to better user experiences.

The history and standards of the DOM

The Document Object Model (DOM) emerged from the growing need to make web pages interactive and dynamically adjustable via scripts. In the early days of the web, HTML was primarily used to display static documents, not for interaction. That changed when browsers began supporting scripting languages like JavaScript.

A brief history of the DOM

During the 1990s, browser vendors developed their own ways of letting scripts manipulate web page content. This led to major inconsistencies in how the DOM functioned across browsers like Netscape Navigator and Internet Explorer. For developers, this meant extra work, scripts had to be adapted for each browser.

To solve this fragmentation, a need for standardization arose. In 1998, the World Wide Web Consortium (W3C) took the initiative to define the DOM as a platform- and language-independent interface. This led to the first official DOM specification: DOM Level 1.

Evolution of the DOM specifications

The standardization of the DOM happened in stages, with each version (or "Level") adding new features:

DOM LevelYearKey Additions
Level 11998Basic structure: document, elements, attributes
Level 22000Style (CSS), events, node traversal
Level 32004Load/save functionality, XML namespace support
Living StandardOngoingContinuous updates via the WHATWG

Since 2015, development of the DOM specification has been managed by the WHATWG (Web Hypertext Application Technology Working Group). Instead of publishing separate versions, they maintain a living standard, an evolving document that reflects the latest browser implementations.

Organizations behind the standard

The main organizations contributing to DOM standardization are:

Thanks to collaboration between these parties, the DOM remains reliable and consistent across modern browsers.

The structure of the DOM tree

The Document Object Model is internally represented as a hierarchical tree structure, commonly referred to as the DOM tree. Every part of a web page, such as a heading, image, or paragraph, is a node in this tree. Together, these nodes form the complete structure of the document, starting from the root node (<html>).

Understanding the tree structure

The DOM tree is similar to a family tree: it’s built on parent-child relationships. The top layer is the document object, followed by the <html> element. Beneath that are <head> and <body>, which contain other elements like <h1>, <p>, and <div>.

A simplified visual example:

document
 └── html
     ├── head
     │    └── title
     └── body
          ├── h1
          └── p

Each of these elements is a node. Nodes can have their own child nodes, forming a nested, tree-like structure.

Nodes: elements, text, and attributes

The DOM distinguishes between several types of nodes. The three most important are:

Example:

<p id="intro">Welcome!</p>

In the DOM, this breaks down into:

How the hierarchy works

The tree structure allows developers to navigate through the document using relationships such as:

Understanding this hierarchy is crucial for working with the DOM using JavaScript. It allows developers to find and manipulate specific parts of a web page efficiently.

DOM and JavaScript

JavaScript is the primary programming language used to manipulate the Document Object Model (DOM). This combination allows developers to dynamically update web pages without reloading them, forming the foundation of interactive websites.

How JavaScript interacts with the DOM

When an HTML page loads, the browser automatically creates a DOM version of it in memory. JavaScript can access and manipulate this structure to read, modify, add, or remove elements. This is done via the document object, which serves as the entry point to the DOM.

Common actions include:

// Select an element by ID
const title = document.getElementById("myTitle");

// Change text content
title.textContent = "Welcome to my website";

// Hide an element
title.style.display = "none";

These actions don’t alter the original HTML file but modify the live version rendered in the browser.

Selecting elements

JavaScript offers several methods to select elements in the DOM:

Example:

const buttons = document.querySelectorAll(".cta-button");

Changing content

You can easily modify content using JavaScript:

document.querySelector("h1").textContent = "New title";
document.querySelector("p").innerHTML = "<strong>Bold text</strong>";

The difference: textContent sets only text, while innerHTML interprets HTML elements.

Adding or removing elements

You can create and insert new elements into the DOM like this:

const newItem = document.createElement("li");
newItem.textContent = "New item";
document.querySelector("ul").appendChild(newItem);

To remove an element:

const toRemove = document.getElementById("oldItem");
toRemove.remove();

Handling events

JavaScript also enables you to respond to user interactions using event listeners:

document.querySelector("button").addEventListener("click", function () {
  alert("Button clicked!");
});

This makes web pages not only visually dynamic but also functionally interactive.

DOM interfaces and objects

The Document Object Model is built around a set of standardized interfaces, these define the structure and behavior of the objects you interact with in the DOM. Thanks to these interfaces, developers can work with elements and their properties consistently across all modern browsers.

What are interfaces in the DOM?

In programming, an interface is a definition of the properties and methods that an object must implement. In the DOM, each type of node is based on one or more interfaces that determine what it can do.

For example:

JavaScript objects in the DOM follow this structure. So a <div> element, for example, has access to methods and properties from Node, Element, and HTMLElement.

Core DOM interfaces

Below are some of the most important DOM interfaces:

InterfaceDescription
NodeThe base class for all nodes in the DOM (elements, text, attributes, etc.)
ElementRepresents a generic HTML or XML element
HTMLElementExtends Element with HTML-specific properties
DocumentRepresents the entire document; the starting point for any DOM interaction
EventRepresents user interactions like clicks, keypresses, or mouse movements

Example:

const button = document.querySelector("button");

In this case, button is an object that implements the HTMLElement interface, which gives it access to methods like addEventListener() and properties like textContent.

Differences between the HTML DOM and SVG DOM

The DOM isn’t limited to HTML. Other types of document structures, such as SVG (Scalable Vector Graphics), have their set of DOM interfaces. This means some methods and properties are specific to SVG elements.

For example:

const circle = document.querySelector("circle");
circle.setAttribute("r", 40); // SVG-specific attribute

While both HTML and SVG elements are accessible through the DOM, they follow different sets of interfaces under the hood.

The structure of DOM objects and their interfaces makes it possible to work with webpages in a reliable, standardized way, essential for modern front-end development.

DOM in practice

The Document Object Model isn’t just a theoretical concept found in developer documentation, it’s a core part of every modern web browser. Thanks to the DOM, web pages can respond to user actions, update content dynamically, and visually adapt without requiring a full-page reload.

Common use cases in browsers

Most of the interactions users experience on websites are made possible through DOM manipulation. Examples include:

All of this is achieved through JavaScript reading and modifying the DOM based on events or data changes.

DOM libraries

While modern browsers provide a powerful native DOM API, many developers use libraries that simplify DOM interactions. The most well-known example is:

Example:

// Native DOM
document.getElementById("title").textContent = "Hello";

// jQuery
$("#title").text("Hello");

Other popular tools and frameworks that interact with the DOM at a higher abstraction level include:

These frameworks manage the DOM internally and optimize updates based on state or data changes.

Browser and layout engine support

All major browsers, like Chrome, Firefox, Safari, and Edge, fully support the DOM according to the living standard. These browsers use different layout engines, such as:

BrowserEngine
ChromeBlink
FirefoxGecko
SafariWebKit
EdgeBlink

These engines are responsible for converting HTML into both a visual representation and a usable DOM tree. Thanks to standardization, the DOM works largely the same across browsers, though minor differences may exist in timing or performance.

Examples

To better understand how the Document Object Model is used in practice, let’s look at two common scenarios: changing text content and adding a new element to the DOM. Each example includes the corresponding HTML and JavaScript so you can easily test or apply them yourself.

Example 1: Changing text content

In this example, a page heading is dynamically updated using JavaScript.

HTML

<h1 id="mainTitle">Welcome to my website</h1>

JavaScript

const title = document.getElementById("mainTitle");
title.textContent = "Text changed via JavaScript";

Result

The original text "Welcome to my website" is immediately replaced with "Text changed via JavaScript" as soon as the script runs.

Example 2: Adding an element to the DOM

Here, we add a new list item to an existing unordered list (<ul>).

HTML

<ul id="shoppingList">
  <li>Bread</li>
  <li>Milk</li>
</ul>

JavaScript

const list = document.getElementById("shoppingList");
const newItem = document.createElement("li");
newItem.textContent = "Eggs";
list.appendChild(newItem);

Result

The list now includes an extra item:

These examples show how powerful and accessible DOM manipulation can be. With just a few lines of JavaScript, you can make web pages dynamic and interactive.

The importance of the Document Object Model

The Document Object Model (DOM) is the backbone of every modern web page. Without the DOM, the web would be static, lacking interaction, responsiveness, and dynamic content. By converting HTML and XML into a structured tree of nodes, browsers enable developers to select, modify, add, or remove elements in real-time.

The DOM allows developers to create user experiences that are intuitive, fast, and engaging. Whether it’s a simple text change or building a full single-page application with frameworks like React, it all starts with the DOM.

For anyone involved in web development, understanding the DOM is essential. It serves as the foundation for the interaction between code and content and is a crucial component of front-end development.

Frequently Asked Questions
What is the Document Object Model?

The Document Object Model (DOM) is a structured representation of a web page, organized as a tree of nodes. It allows developers to use JavaScript to dynamically read and modify the content of a web page.


What is the difference between HTML and the DOM?

HTML is the markup language written in the source code of a web page. The DOM is the live, in-memory representation of that HTML in the browser. JavaScript interacts with the DOM, not directly with the HTML file.


What are the three parts of the DOM?

The three main parts of the DOM are element nodes, text nodes, and attribute nodes. Element nodes represent HTML elements like <div> or <p>, text nodes contain the actual text within those elements, and attribute nodes hold additional information such as id, class, or src values.


Articles you might enjoy

Piqued your interest?

We'd love to tell you more.

Contact us
Tuple Logo
Veenendaal (HQ)
De Smalle Zijde 3-05, 3903 LL Veenendaal
info@tuple.nl‭+31 318 24 01 64‬
Quick Links
Customer Stories