In Keep Track Between Clicks, drawPlayers() gave every guest a Check in button. Each pass of the loop created a button and stored it in that pass’s const button. Once the loop ends, nothing else in the script has a name for Jonas’s button, and the Console never had one.
The rest of this section works on elements that are already on the page. You’ll mark guests who’ve arrived, switch off buttons, and hide a guest who cancels. In this lesson, you’ll find elements without giving each one an id, find every element that matches at once and change each of them, and collect what they show into an array. Start with a question you can already half answer. What else can querySelector() take besides an id?
Find Elements With Any Selector
So far, you’ve found elements with # and an id, such as document.querySelector("#players"). The Check in buttons have no ids, and giving every button, name, and list item one would mean inventing three ids per guest. You don’t need to, because querySelector() takes any CSS selector, the same kind you write in style.css.
You’ll type every step in this lesson into the Console. First, get a page with Priya’s players on it. Follow the route that matches what you have.
If you have Priya’s files, start Live Preview on scratch/index.html in VS Code, check that the address starts with http:// and ends with /scratch/index.html, and open the Console with Ctrl+Shift+J, or Cmd+Option+J on macOS. Names declared in main.js, such as playerList, stay inside the module, so the Console can’t use them. Each step below finds what it needs from document instead.
If you joined the course here, open a new tab, go to about:blank, and open the Console with the same keys. Type each entry rather than pasting it, because Chrome blocks a first paste into the Console as a safety measure. Start with the guests and a list with Priya’s id:
> const players = [
… { name: "Amara", isNew: false },
… { name: "Jonas", isNew: true },
… { name: "Wei", isNew: false },
… { name: "Sam", isNew: true },
… ];
undefined
> const playerList = document.createElement("ul");
undefined
> playerList.id = "players";
'players'
> document.querySelector("body").append(playerList);
undefined
Next, build one list item per guest, as Priya’s drawPlayers() does. Each item holds a span for the name and a Check in button beside it, and a first-timer’s item gets the class new. Press Shift+Enter between the lines of the loop:
> for (const player of players) {
… const item = document.createElement("li");
… const name = document.createElement("span");
… name.textContent = player.name;
… item.append(name);
… const button = document.createElement("button");
… button.textContent = "Check in";
… item.append(button);
… if (player.isNew) {
… item.classList.add("new");
… }
… playerList.append(item);
… }
undefined
Priya’s page also has a Count me in button under the list and a style rule that lines each button up beside its name. Add both:
> const countMeInButton = document.createElement("button");
undefined
> countMeInButton.id = "count-me-in";
'count-me-in'
> countMeInButton.textContent = "Count me in";
'Count me in'
> document.querySelector("body").append(countMeInButton);
undefined
> const style = document.createElement("style");
undefined
> style.textContent =
… "#players li { display: flex; gap: 0.5rem; }";
'#players li { display: flex; gap: 0.5rem; }'
> document.querySelector("head").append(style);
undefined