In Step Through the Page Tree, Jonas’s Check in button reached his list item with closest("li"), and the handler read his name from the span. That’s enough to say who clicked. On game night, though, Priya needs more from each row. A guest who clicks Check in twice shouldn’t count twice, Wei’s friend has to be counted too, and if Jonas cancels, his row should leave the page.
In this lesson, you’ll switch a button off and on and check the change in the Elements panel. You’ll store each guest’s ID and party size on their button and read them back in the handler. You’ll also hide a guest’s row and find out why a stylesheet rule can stop that from working. Start with the button, since switching it off is the change Priya needs first.
Switch a Button Off and On
A button that has been clicked once should stop taking clicks, so nobody is counted twice. HTML has a setting for that, and your script can read and change it. 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 and check that the address starts with http:// and ends with /scratch/index.html. Then open the Console with Ctrl+Shift+J, or Cmd+Option+J on macOS. Reload the page so every button reads Check in again. Names declared in main.js stay inside the module, so the Console finds what it needs from document.
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. Press Shift+Enter between the lines of an entry. Start with Priya’s guests:
> const players = [
… { id: "amara", name: "Amara", isNew: false, party: 1 },
… { id: "jonas", name: "Jonas", isNew: true, party: 1 },
… { id: "wei", name: "Wei", isNew: false, party: 2 },
… { id: "sam", name: "Sam", isNew: true, party: 1 },
… ];
undefined
Each record has two fields that Priya’s page gains later in this lesson. The id is a short name for code to use, and party is the total number of people in the guest’s group. Wei is bringing a friend, so Wei’s party is 2. Next, add a list with the ID players and the style rule that lines each button up beside its name:
> const playerList = document.createElement("ul");
undefined
> playerList.id = "players";
'players'
> document.querySelector("body").append(playerList);
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
Last, build one list item per guest, as Priya’s drawPlayers() does, with the name in a span, a Check in button beside it, and the class new on a first-timer’s item:
> 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
The tab now shows Amara, Jonas, Wei, and Sam, each with a Check in button. Keep it open for the whole lesson, because a reload clears everything you typed.
On either route, keep Wei’s button in a constant. It’s the third button in the players list, at position 2, and every button element has a disabled property:
> const weiButton = document.querySelectorAll("#players button")[2];
undefined
> weiButton.disabled
false
Now switch the button off by setting the property to true:
> weiButton.disabled = true;
true
Wei’s button turns gray, and clicking it does nothing. On Priya’s page, a click no longer logs Checked in: Wei, because a disabled button doesn’t fire click events.
Open the Elements panel and select Wei’s button. Its start tag now reads <button disabled>. That’s an attribute, a setting written in the element’s HTML tag, like the id and class attributes you’ve written in index.html. What you changed was a property, a value on the element object your script holds. For most attributes, the browser keeps a matching property in step with the attribute, so changing one changes the other.
The two don’t always look alike, though. Every element has getAttribute(), which reads an attribute’s value as text. Read Wei’s disabled attribute:
> weiButton.getAttribute("disabled")
''
The attribute’s value is an empty string, while the property is true. Before you switched the button off, getAttribute("disabled") would have given null, because the attribute wasn’t there at all. For disabled, only whether the attribute is present counts. Any value, even an empty one, means disabled.