1 . What is the DOM?
2 . Why You Should Learn Vanilla DOM Manipulation
3 . Accessing DOM Elements
4 . Modifying Elements and Content
5 . Creating and Inserting New Elements
6 . Removing and Replacing Elements
7 . Working with Attributes and Properties
8 . Event Handling and Listeners
9 . Class Manipulation and Styling
10 . Real-World Examples of DOM Manipulation
11 . Performance Considerations
12 . Debugging and Developer Tools
13 . Best Practices
Conclusion
The Document Object Model (DOM) is at the core of every webpage. It represents your page so JavaScript can interact with, modify, and enhance it dynamically. While modern JavaScript frameworks like React and Vue have abstracted much of this process, understanding vanilla DOM manipulation is an essential skill for every front-end developer.
In this guide, we’ll take a deep dive into everything you need to know about DOM manipulation using pure JavaScript, empowering you to build dynamic, interactive web applications, no frameworks required.
The DOM (Document Object Model) is a programming interface for HTML and XML documents. It represents the page so that programs (like JavaScript) can change the document structure, style, and content.
Think of it as a tree-like structure where each HTML tag becomes a node, and JavaScript can traverse, update, add, or delete these nodes.
Example HTML:
<body>
<div id="app">
<h1>Hello, world!</h1>
</div>
</body>
DOM Tree Representation:
Even though frameworks handle DOM interactions behind the scenes, understanding native DOM manipulation gives you:
You don’t need React to make something interactive. Pure JavaScript is more than capable.
JavaScript provides several methods for selecting DOM elements:
Examples:
const title = document.getElementById("main-title");
const listItems = document.querySelectorAll("li");
const container = document.querySelector(".container");
querySelector/querySelectorAll uses CSS-style selectors and is most flexible.
You can change text content, HTML content, and more.
Text Content:
const title = document.querySelector("h1");
title.textContent = "New Title!";
HTML Content:
const container = document.querySelector(".box");
container.innerHTML = "<p>New HTML Content</p>";
Value (for form inputs):
const input = document.querySelector("#name");
input.value = "John Doe";
You can dynamically create new elements using:
Example:
const newDiv = document.createElement("div");
newDiv.textContent = "I'm a new div!";
document.body.appendChild(newDiv);
Modern methods:
document.body.append("Appended content");
document.body.prepend("Prepended content");
To remove elements:
const element = document.querySelector("#to-remove");
element.remove();
** To replace:**
const oldElement = document.querySelector("#old");
const newElement = document.createElement("div");
newElement.textContent = "New Element";
oldElement.replaceWith(newElement);
You can access, set, and remove attributes:
const link = document.querySelector("a");
link.getAttribute("href"); // Get
link.setAttribute("target", "_blank"); // Set
link.removeAttribute("target"); // Remove
Properties are accessed directly:
const input = document.querySelector("input");
input.value = "Updated Value";
input.disabled = true;
Attach event listeners to elements using:
const button = document.querySelector("button");
button.addEventListener("click", () => {
alert("Button clicked!");
});
You can also define the callback separately:
function handleClick() {
console.log("Clicked!");
}
button.addEventListener("click", handleClick);
Event Types:
You can toggle, add, remove classes dynamically:
const box = document.querySelector(".box");
box.classList.add("active");
box.classList.remove("hidden");
box.classList.toggle("highlight");
Inline styling:
box.style.backgroundColor = "blue";
box.style.fontSize = "1.5rem";
Interactive To-Do List:
const input = document.querySelector("#taskInput");
const btn = document.querySelector("#addTask");
const list = document.querySelector("#taskList");
btn.addEventListener("click", () => {
const li = document.createElement("li");
li.textContent = input.value;
list.appendChild(li);
input.value = "";
});
Form Validation:
form.addEventListener("submit", (e) => {
e.preventDefault();
if (!input.value) {
errorMsg.textContent = "Field cannot be empty";
}
});
Light/Dark Theme Toggle:
toggleBtn.addEventListener("click", () => {
document.body.classList.toggle("dark-mode");
});
DOM operations are expensive. Minimize repaints and reflows:
Example:
const fragment = document.createDocumentFragment();
for (let i = 0; i < 100; i++) {
const li = document.createElement("li");
li.textContent = `Item ${i}`;
fragment.appendChild(li);
}
document.querySelector("ul").appendChild(fragment);
Modern browsers come with robust tools for inspecting and debugging DOM elements:
const el = document.querySelector("h1");
console.dir(el);
Minimize direct DOM manipulation in complex projects.
DOM manipulation is the foundation of web interactivity. By understanding how to use vanilla JavaScript to interact with the DOM, you unlock the ability to build rich, dynamic experiences without relying on external frameworks.
Whether you're building a small feature or a full-blown application, these skills allow you to:
Start small. Experiment. Try rebuilding a framework feature in vanilla JS. You’ll not only become a better developer, you’ll gain full control over your web projects.
Master the DOM, and the browser becomes your playground.
Software Engineer
Senior Software Engineer