
web-development · Intermediate · 2026-07-04
JavaScript DOM Manipulation: Make Your Page Interactive
Learn how JavaScript reads and changes a web page — select elements, change text and styles, create new elements and react to user events.
The DOM (Document Object Model) is the browser's live, in-memory version of your HTML. When JavaScript "changes the page", it is really changing the DOM — and the browser instantly redraws what you see.
Selecting elements
Before you can change something, you have to find it:
// By id — returns one element
const title = document.getElementById("title");
// By CSS selector — returns the first match
const firstButton = document.querySelector("button");
// All matches — returns a list
const allItems = document.querySelectorAll("li");
querySelector accepts any CSS selector, so everything you learned in CSS works here too: .class, #id, ul > li, and so on.
Changing content and styles
title.textContent = "New heading"; // change the text
title.style.color = "crimson"; // change one style
title.classList.add("highlight"); // add a CSS class (better!)
title.classList.toggle("dark-mode"); // switch a class on/off
Prefer classList over style — keep the styling rules in your CSS file and just switch classes from JavaScript.
Creating and removing elements
const li = document.createElement("li"); // create
li.textContent = "Buy milk"; // fill it
list.appendChild(li); // attach to the page
li.remove(); // delete it again
Nothing appears on the page until you attach the new element with appendChild (or append, prepend, insertBefore).
Reacting to events
Events are how the page tells you something happened:
button.addEventListener("click", function () {
console.log("Button was clicked!");
});
Common events you will use all the time:
| Event | Fires when... |
|---|---|
click | An element is clicked |
input | Text in a field changes |
submit | A form is submitted |
keydown | A keyboard key is pressed |
mouseover | Pointer enters an element |
Try it yourself: a mini shopping list
The editor below combines everything: it selects the input, button and list, then on every click it creates a new <li>, fills it with the input text, and attaches it to the list. Clicking any item removes it.
Try these challenges:
- Make the Enter key also add the item (listen for
keydownon the input and checkevent.key === "Enter"). - Show an alert when the user tries to add an empty item.
- Add a counter above the list showing how many items it contains.
- Bonus: give every added item a random colour using
li.style.color.
Press Run after each change to see the result instantly.
Practice exercises
Still using the shopping list — these three exercises each practise one core DOM skill.
Exercise 1 (easy): Add a "Clear all" button to the HTML that empties the entire list at once. (Hint: setting list.innerHTML = "" removes every child.)
Solution:
<button id="clear-btn">Clear all</button>
document.getElementById("clear-btn").addEventListener("click", function () {
list.innerHTML = "";
});
Exercise 2 (medium): Right now, clicking an item deletes it. Change the behaviour: the first click should mark it done (strike-through), a second click deletes it. Use classList — add a .done rule in the CSS tab with text-decoration: line-through.
Solution:
.done { text-decoration: line-through; color: #999; }
li.addEventListener("click", function () {
if (li.classList.contains("done")) {
li.remove();
} else {
li.classList.add("done");
}
});
Exercise 3 (challenge): Show a live item count in the page's <h2> — "My Shopping List (3)". Update it every time an item is added or removed. (Hint: list.children.length gives the current count; write an updateCount() function and call it after every change.)
Solution:
const heading = document.querySelector("h2");
function updateCount() {
heading.textContent = "My Shopping List (" + list.children.length + ")";
}
// call updateCount(); after appendChild and after remove
Practise what you learned
Edit the code below and press Run to see your changes live.
Test yourself
Answer from memory first, then check yourself against the answer.
Q1What exactly is the DOM?
The Document Object Model — the browser's live, in-memory representation of your HTML. JavaScript never edits the HTML file itself; it changes the DOM, and the browser instantly redraws what you see.
Q2When would you use querySelector instead of getElementById?
getElementById only finds elements by id. querySelector accepts any CSS selector — classes (.card), nesting (ul > li), attributes — so it can target anything your CSS can. Use getElementById when you have an id; it is slightly faster and the intent is clearer.
Q3Why is classList.add() better than setting element.style directly?
classList keeps the styling rules in your CSS file where they belong — JavaScript just switches a named state on or off. Inline styles scatter design decisions through your logic and override your stylesheet, making both harder to maintain.