Switch Classes and Styles

At the end of Read and Set Attributes and Data, each Check in button switched itself off once its guest arrived, and Jonas’s row could leave the page and come back. Hiding is all or nothing, though. On game night, Priya wants every row to stay on the page, with the guests who’ve arrived standing out at a glance.

In this lesson, you’ll make each Check in button mark its guest’s row with a class, so the stylesheet decides how a guest who’s arrived looks. You’ll switch that class off and on from the Console, set a single style from JavaScript where a class can’t do the job, and read back the styles an element really has. Start with a CSS rule.

Mark Arrivals With a Class

Priya’s stylesheet already shows one kind of guest differently. The #players li.new rule in style.css defines once how a first-timer looks, and drawPlayers() adds the class new to the right items. A class used like this, to name a state an element is in, is a state class. Having arrived is another state, so it gets its own class and its own rule.

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.

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 and a list with the players ID:

Language: JavaScript
> 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

Priya’s style.css stores her accent color once, as the custom property --accent, and the rule for new players reads it with var(--accent). Another rule lines up each button beside the guest’s name. A <style> element holds CSS in the page itself, so add all three rules in one:

Language: JavaScript
> const style = document.createElement("style");
undefined
> style.textContent = `
… :root { --accent: crimson; }
… #players li { display: flex; gap: 0.5rem; }
… #players li.new { color: var(--accent); font-weight: bold; }
… `;
… document.querySelector("head").append(style);
undefined

The list is still empty. You’ll fill it with drawPlayers() in a moment. Keep this tab open for the whole lesson, because a reload clears everything you typed.

On either route, start with the look. If you have Priya’s files, add this rule to the end of scratch/style.css:

Language: CSS Filename: scratch/style.css
#players li.arrived {
  background-color: #dcfce7;
}

If you joined the course here, add the same rule in a second <style> element:

Language: JavaScript
> const arrivedStyle = document.createElement("style");
undefined
> arrivedStyle.textContent =
…   "#players li.arrived { background-color: #dcfce7; }";
'#players li.arrived { background-color: #dcfce7; }'
> document.querySelector("head").append(arrivedStyle);
undefined

Now the Check in handler only has to add the class. If you have Priya’s files, open scratch/main.js, find the Check in arrow function in drawPlayers(), and add one line after button.disabled = true;:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.