HTML describes what a page looks like when it first loads. But the moment you click a button and something changes without the page reloading, that's JavaScript reaching into the page and altering it directly, live, through something called the DOM.

What the DOM actually is

DOM stands for Document Object Model, which sounds more intimidating than it's. When a browser loads your HTML, it builds a tree of objects representing every element on the page — every div, every button, every paragraph becomes an object JavaScript can read and change. Change the object, and the browser immediately updates what you see on screen.

Finding an element

const heading = document.querySelector("h1");
console.log(heading.textContent);

querySelector takes a CSS-style selector, the exact same kind you would write in a stylesheet, and returns the first matching element it finds. Prefer querySelectorAll when you want every match instead of just the first.

Changing what is on the page

heading.textContent = "Welcome!";
heading.style.color = "blue";
heading.classList.add("highlight");

textContent swaps the visible text. style lets you tweak CSS directly from JavaScript, though for anything more than a quick one-off change, toggling a CSS class with classList is usually the cleaner habit, since your styling stays organised in a stylesheet rather than scattered through your JavaScript.

Reacting to a click

const button = document.querySelector("button");
button.addEventListener("click", function () {
  alert("Button clicked!");
});

addEventListener is the workhorse of interactive pages. You're telling the browser: "when this specific thing happens to this specific element, run this specific function." The function itself doesn't run right away — it just sits there, waiting, until the click actually happens.

Creating brand new elements

const item = document.createElement("li");
item.textContent = "New item";
document.querySelector("ul").appendChild(item);

This pattern, create an element, fill it with content, then attach it somewhere in the page, is how JavaScript builds things like a new row in a to-do list without ever reloading the page.

A word of caution

Directly manipulating the DOM by hand, piece by piece, works perfectly well for small pages, but it gets messy fast once a page has a lot of moving parts. This is exactly the problem frameworks like React were built to solve, by letting you describe what the page should look like and handling the actual DOM updates for you behind the scenes. That said, understanding plain DOM manipulation first makes those frameworks make a lot more sense later, rather than feeling like unexplained magic.