Web DesignMaking it moveLesson 8 / 69

Selecting & changing the DOM

Exercise35 min

Selecting & changing the DOM

The browser exposes the page to JavaScript through the DOM — a live tree of objects. document.querySelector finds one; the object can be changed:

const title = document.querySelector("h1");
title.textContent = "Changed by JS";
title.style.color = "tomato";

You can also build elements and add them:

const p = document.createElement("p");
p.textContent = "I did not exist a second ago";
document.body.append(p);

This loop — find, change, create — is what "interactive page" means. Later, React will manage this tree for you, but the model underneath stays the same.

Exercise

Add a button. When clicked, append a new paragraph with a random number to the page.

Practice in the diagram lab

This lesson's practice belongs in the diagram editor — design the schema you just read about, grade it against the solution, and save it to your account.

Open the diagram lab