Understanding DOM
DOM: Document Object Model
When you interact with a webpage — click a button, fill a form, or change content — you are actually talking to the DOM.
What is DOM?
The DOM (Document Object Model) is a programmatic representation of your webpage.
It takes the HTML document and turns it into a structured, editable “tree” of objects that a programming language like JavaScript can understand and manipulate.
The DOM isn’t just a static map; it’s an API (Application Programming Interface). This means you can use JavaScript to “talk” to the DOM.
Get Nimmikrishnab’s stories in your inbox
Join Medium for free to get updates from this writer.
Process:
- Parsing HTML: The browser reads your HTML file from top to bottom.
- Tree Construction: For every tag (like
<div>,<h1>, or<p>), the browser creates a "node." These nodes are linked together in a parent-child relationship. - The Result: You get a DOM Tree. The
documentis the root,<html>is the trunk, and every other element is a branch or a leaf.
- Selecting elements
const heading = document.querySelector('h1');2. Manipulating Elements
Once you have the element, you can change its properties (like text, color, or position):
heading.textContent = "Welcome to my Medium Article!";
heading.style.color = "blue";3. Event Listening
Tell the DOM to “watch” for certain user actions:
button.addEventListener('click', () => {
console.log("Button was clicked!");
});Without the DOM, JavaScript would have no way of knowing what is happening on your website. It provides the bridge between your code and the user’s interface. Every time you see a “Dark Mode” toggle, a popup modal, or a live search bar, you are seeing the DOM being manipulated in real-time.
Modern JavaScript frameworks like React and Vue use the DOM under the hood, often with virtual DOMs to improve performance.







