HTML, CSS & JS, lesson 7
JavaScript makes pages react. In the browser it can read and change the page through the DOM (Document Object Model), a tree of objects that mirrors your HTML.
querySelector takes the same selectors as CSS, so "#total", ".card p" and "li" all work.
It returns null if nothing matches.
Prefer textContent for text. It treats what you give it as plain text, so a user can't inject
HTML into your page.
Pages run your code when something happens: a click, a key press, a form submit. Attach a
function with addEventListener:
The function you pass is called every time the event fires. Variables declared outside it, like , keep their values between clicks.
One exercise to try what you just read.
likesFor forms, listen for "submit" on the <form> and call event.preventDefault() to stop the
browser from reloading the page.
Make the counter work:
#add increases the number shown in #count by 1.#reset sets it back to 0.full to #count, and remove it again on reset.const title = document.querySelector("h1"); // first match
const buttons = document.querySelectorAll("button"); // every matchtitle.textContent = "Order received!";
title.classList.add("done"); // also .remove(), .toggle(), .contains()
title.style.color = "green"; // inline style, best for one-off changesconst button = document.querySelector("#like");
let likes = 0;
button.addEventListener("click", () => {
likes += 1;
button.textContent = `Likes: ${likes}`;
});