Skip to content

What is the DOM?

Updated Nov 30, 20263 min readBased on official documentation
What is the DOM?

In short

The DOM, or Document Object Model, is a programming interface for web pages. It represents a page as a tree of objects so that programs, usually JavaScript, can read and change its structure, style and content. The browser builds it from your HTML when the page loads.

What the DOM is

The Document Object Model, usually shortened to DOM, is a way for code to work with a web page. A web page is written in HTML, which is plain text. The DOM takes that text and turns it into a set of objects that a program can read and change. Once the page is in this form, a script can find a paragraph, change a heading, add a button or remove an image while the page is open.

The DOM is not a programming language. It is a Web API, meaning a set of built-in tools that browsers provide. Most developers use it through JavaScript, but the idea is not tied to one language. Implementations exist for other languages too, such as Python, which can read XML documents in a similar way.

How it works

When a browser loads an HTML document, it reads the markup and builds a DOM tree. A tree is a structure where each item has a parent and possibly children. Every piece of the page becomes a node in this tree: the whole document, each element such as a heading or a paragraph, the text inside those elements, and attributes.

Take a small page with a title, a heading and a paragraph. The root is the html element. It has two children, head and body. The body has an h1 and a p, and each of those holds its own text. The browser uses this tree to decide what to draw on the screen. Your code can use the same tree to find and change any part of it.

The DOM also keeps all of this in objects. The document object stands for the whole page. Each element is an object with properties, such as its text or its attributes, and methods, which are actions you can call on it. Because they are grouped this way, you can work with each part of the page in a consistent manner.

A simple example

Here is a short script that adds a new heading to a page. It creates an h1 element, gives it some text and adds it to the end of the body.

const heading = document.createElement("h1");
const headingText = document.createTextNode("Big Head!");
heading.appendChild(headingText);
document.body.appendChild(heading);

The script first creates the element, then creates a text node, then attaches the text to the element. The last line places the element into the tree, and the browser shows it right away. You can also select existing elements. The call document.querySelectorAll("p") returns a list of all paragraph elements on the page, which you can loop through and change.

When you use it

You use the DOM whenever a page needs to respond to the user or to new data. Common cases include showing or hiding a menu, validating a form before it is sent, updating a counter, or loading extra content without reloading the page. You do not need any special setup to start. You write a script, either inline inside a script element or in a separate file, and it can use the DOM straight away.

It is generally a good idea to keep the structure of the page in HTML and the changes in JavaScript. Keeping them apart makes the code easier to read and to maintain.

Common mistakes

A frequent mistake is running a script before the element it needs exists. If the script runs while the browser is still reading the HTML, a query may return nothing. Place the script at the end of the body, or wait for the page to finish loading.

Another mistake is assuming that every node is an element. Text and attributes are also nodes. Some methods return elements, while others return text nodes, so check what you get back before you use it.

Finally, remember that the DOM is not the same as the HTML source. Changes you make through the DOM happen in the live page. The original HTML file stays the same, and reloading the page brings the original structure back.

The DOM works closely with CSS, which controls how elements look, and with events, which tell your code when a user clicks or types. Together, these three form the core of how interactive pages are built in the browser. The DOM itself is defined by a set of interfaces, such as Element, Node and Document, and the browser provides objects that follow them.

To learn more, read the official documentation on MDN, which describes the interfaces in detail.

FAQ

Is the DOM the same as HTML?

No. HTML is the text you write for a page. The DOM is the live tree of objects the browser builds from that text, and scripts can change it while the page is open.

Do I need to install anything to use the DOM?

No. Browsers provide the DOM, so you can use it from any script that runs in the page. You only need a text editor and a browser.

Is the DOM part of JavaScript?

No. The DOM is a Web API that browsers provide. JavaScript is the most common language used to work with it, but the DOM is separate from the language itself.

Can I use the DOM outside the browser?

Some tools implement the DOM for other settings. Node.js, for example, does not include the DOM as a core part of its runtime, so you need a separate library for it.

Sources

This page is based on the official documentation below. Read it for full detail.

DOM — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.