1. Find an element and change it
The browser turns your HTML into a tree of objects called the DOM. JavaScript can find any element and change it:
document.querySelector("#title")finds the element withid="title"(use.namefor a class, like CSS)el.textContent = "New text";changes its textel.classList.add("highlight");adds a CSS class (alsoremoveandtoggle)
Now your code changes what people see. Try it — the preview updates when you run.
const heading = document.querySelector("h1");
heading.textContent = "Jai Hind!";
heading.classList.add("big");
Challenge: Namaste, India!
Use querySelector to:
- Change the text of
#titletoNamaste, India! - Add the class
highlightto the paragraph with classmsg