DEV Community

Cover image for What Is the DOM? How JavaScript Interacts with Web Pages (Beginner Guide)
Vamsi Krishna Budati
Vamsi Krishna Budati

Posted on

What Is the DOM? How JavaScript Interacts with Web Pages (Beginner Guide)

Introduction

When learning JavaScript, you’ll often hear this line:

JavaScript interacts with the webpage through the DOM.

For beginners, this can feel confusing:

In this blog, you'll understand the DOM from scratch with examples and how JavaScript brings web pages to life.

Tree Like Structure

What is the DOM?

DOM stands for Document Object Model.

This is the structured(tree-like) representation of your HTML page that the browser creates so JavaScript can interact with it.

HTML vs DOM

Let's clear very common confusion.

  • HTML is a static text file
  • DOM is a dynamic, a tree like structure created by the browser

Example:

<h1>Hello</h1>
<p>How is it going?</>
Enter fullscreen mode Exit fullscreen mode

The browser turns this into a tree like structure:

document
    |__ html
         |__ body
               |
            p__|__ h1
Enter fullscreen mode Exit fullscreen mode

DOM is stored in-memory by the browser.

DOM allows JavaScript to access and manipulate the elements/content in the webpage.

Each element in a DOM could be a node and each node could have a child node.

Example:

  • <body> is the parent node
  • <p> & <h1> are its child nodes

How JavaScript Interacts with the DOM

JavaScript uses built-in browser APIs to read and manipulate the DOM.

Let’s look at what JavaScript can actually do.

Access Elements on the Page

const heading = document.querySelector("h1");
Enter fullscreen mode Exit fullscreen mode

Change Content Dynamically

heading.textContent = "Welcome to JavaScript!";
Enter fullscreen mode Exit fullscreen mode

Change Styles Using JavaScript

heading.style.color = "blue";
Enter fullscreen mode Exit fullscreen mode

Respond to User Actions

document.querySelector("button").addEventListener("click", () => {
  alert("Button clicked!");
});
Enter fullscreen mode Exit fullscreen mode

Create and Remove Elements

const p = document.createElement("p");
p.textContent = "This is a new paragraph";
document.body.appendChild(p);
Enter fullscreen mode Exit fullscreen mode

Conclusion

Let’s summarize:

  • The DOM is a browser-created structure representing your HTML
  • It is organized like a tree
  • JavaScript uses the DOM to:
    • Read content
    • Update text and styles
    • Respond to user actions
    • Add or remove elements dynamically
  • This is how web pages become interactive and dynamic

Once you understand the DOM, JavaScript will make much more sense.

References & Further Reading

MDN Web Docs – DOM Introduction

MDN Web Docs – Manipulating Documents

MDN Web Docs – DOM Overview

Top comments (1)

Collapse
 
a-k-0047 profile image
ak0047

Really clear and beginner friendly article — thank you!