A website for the Purrfect Café
The DOM tree, querySelector, textContent, createElement, classList and dataset.
The browser doesn't keep a page as text. It reads the HTML and builds a tree of objects out of it — the DOM (Document Object Model). Every tag becomes an element object:
- at the top is document, under it
<html>, inside that<head>and<body>; - every element has a parent (parentElement) and children (
children); - an element is an ordinary JavaScript object with properties and methods.
Change the object — and the browser redraws the page right away. That's the whole trick.
const title = document.querySelector("#title");
console.log(title.textContent);
const menu = document.querySelector("#menu");
console.log(menu.children.length);
console.log(menu.parentElement.tagName);
console.log(menu.children[0].textContent);Elements are found with a CSS selector — the same language you use in styles:
#menu— byid;.dish— by class;li— by tag,#menu li— anliinside#menu.
querySelector returns the first matching element (top to bottom on the page), or null if there are none. querySelectorAll returns all of them, as a NodeList. It isn't an array, but you can loop over it with for...of, and [...list] turns it into a real array with map and filter.
const first = document.querySelector(".dish");
console.log(first.textContent);
const all = document.querySelectorAll(".dish");
console.log(all.length);
console.log([...all].map((li) => li.textContent));
console.log(document.querySelector(".pizza"));null: the cat café doesn't serve pizza.Now let's change the page:
el.textContent = "…"— replace all the text inside an element;document.createElement("li")— create a new element. For now it's floating in the air: it isn't on the page;parent.append(el)— insert it at the end of the parent. If the element was already somewhere on the page, append moves it rather than copying it;el.remove()— take an element off the page.
const menu = document.querySelector("#menu");
document.querySelector("#title").textContent = "The Purrfect Café — now open!";
const li = document.createElement("li");
li.textContent = "Tiramisu";
menu.append(li);
menu.children[0].remove();
console.log(menu.children.length);
console.log([...menu.children].map((item) => item.textContent));The look of things is usually set with classes, and CSS already knows how to draw them. el.classList is the element's set of classes: add, remove, contains and toggle (there — remove it, not there — add it; toggle("x", condition) adds it only if the condition is true).
You can keep your own data right in the markup, in data-* attributes: <li data-price="4">. In JavaScript they live in el.dataset, and data-sold-out becomes dataset.soldOut. Any attribute can be read and written with getAttribute / setAttribute.
const [latte, cheesecake] = document.querySelectorAll("#menu li");
cheesecake.classList.add("sold-out");
console.log(cheesecake.classList.contains("sold-out"));
latte.classList.toggle("hit");
console.log(latte.className);
console.log(latte.dataset.price, typeof latte.dataset.price);
console.log(cheesecake.dataset.soldOut);
console.log(latte.getAttribute("data-price"));data-*. Notice: the price from dataset is a string.And now the main move of the whole browser part of this course: data → DOM. The menu lives in an array of objects, and a function draws the page from it: empty the list (replaceChildren() with no arguments removes every child) and create one element per object. The data changed? Call the function again.
const dishes = [
{ name: "Latte", price: 4 },
{ name: "Cheesecake", price: 3 },
{ name: "Cocoa", price: 5 },
];
function renderMenu(items) {
const menu = document.querySelector("#menu");
menu.replaceChildren();
for (const dish of items) {
const li = document.createElement("li");
li.textContent = `${dish.name} — $${dish.price}`;
menu.append(li);
}
}
renderMenu(dishes);
renderMenu(dishes);
console.log(document.querySelectorAll("#menu li").length);createElement, textContent and append. Get the hang of these — and frameworks stop being magic.Pop quiz
Mistakes are fine: keep trying until you find the right answer, and Byte explains why it's right.