Skip to content
Happy Programming Guide
Start learning
JavaScript

JavaScript DOM Basics: Changing a Web Page

How to select elements, change text and styles, add and remove elements, and why your script sometimes cannot find anything on the page.

The DOM is the browser’s live model of your page. Your HTML is the starting instructions; the DOM is what actually exists once the page has loaded — and JavaScript can change it.

JavaScript
document.querySelector("h1").textContent = "Changed!";

Type that into your browser console on any page and the heading changes immediately.

Selecting elements#

JavaScript
document.querySelector("#total");        // first match by id
document.querySelector(".card");         // first match by class
document.querySelector("ul li");         // first li inside a ul
document.querySelectorAll(".card");      // all matches

querySelector takes any CSS selector, which means one thing to learn instead of five. It returns null if nothing matches — and that null is behind most beginner DOM errors.

Changing content#

JavaScript
const title = document.querySelector("#title");

title.textContent = "Hello";                   // safe — treats it as plain text
title.innerHTML = "<strong>Hello</strong>";    // parses HTML

Attributes and form values#

JavaScript
const img = document.querySelector("img");
img.src = "photo.jpg";
img.alt = "A description";

const input = document.querySelector("#age");
console.log(input.value);            // always a string
console.log(Number(input.value));    // convert before doing maths

const link = document.querySelector("a");
link.setAttribute("target", "_blank");

Form inputs always give you text, even type="number" ones. That is the DOM equivalent of Python’s input() trap.

Styles and classes#

JavaScript
const box = document.querySelector(".box");

box.style.backgroundColor = "tomato";   // inline style — fine for one-offs

box.classList.add("is-active");         // better: let CSS do the work
box.classList.remove("is-hidden");
box.classList.toggle("is-open");
box.classList.contains("is-open");      // true / false

Prefer toggling classes over setting styles directly. It keeps the appearance in your CSS file where it belongs.

Creating and removing elements#

JavaScript
const list = document.querySelector("#todo-list");

const item = document.createElement("li");
item.textContent = "Learn the DOM";
item.classList.add("todo");
list.appendChild(item);

item.remove();                 // take it out again
list.innerHTML = "";           // clear everything inside

Building a list from data#

JavaScript
const tasks = ["Study", "Code", "Rest"];
const list = document.querySelector("#tasks");

list.innerHTML = "";
for (const task of tasks) {
  const li = document.createElement("li");
  li.textContent = task;
  list.appendChild(li);
}

Keep your data in an array and re-render from it. Trying to keep the DOM and your data in sync by hand is where things get messy.

Walking the tree#

JavaScript
const item = document.querySelector(".card");

item.parentElement;
item.children;
item.nextElementSibling;
item.closest(".container");   // nearest matching ancestor

closest() is genuinely useful when handling clicks — you can find the card that contains the button that was pressed.

Questions people ask#

What is the difference between textContent and innerText?

textContent returns everything including hidden text and is faster. innerText reflects what is visually rendered. Use textContent unless you specifically need the visual version.

querySelectorAll returns something odd — why can I not use map on it?

It returns a NodeList, not an array. Convert it with [...document.querySelectorAll(".card")] and then all the array methods work.

Do I need jQuery?

No. Modern browsers give you querySelector, classList and fetch, which cover what jQuery was mainly used for.

Where to go next#

Next lessonJavaScript events explained

Keep reading

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *