Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Make Controls Work for Everyone

Sam’s list already had a / shortcut. In this video, you tested the whole shopping list with the keyboard alone, added a Show only today toggle that tells a screen reader whether it’s on, and fixed a shared list snippet Sam copied from an old site. Here are the key points:

  • Test with the keyboard alone. Some people can’t use a mouse, and many people using a screen reader, software that reads the page aloud, work from the keyboard too. Each press of Tab moves the focus ring, the outline around the control your next key goes to. A radio group is one stop, and the arrow keys pick inside it. Space ticks a box, and Enter on a button presses it.
  • Native elements work from the keyboard for free. The browser already knows how to focus and use a real <input>, <select> or <button> with a label.
  • The Accessibility tab shows what a screen reader is told. Right-click an element and choose Inspect, then open the Accessibility tab in the Elements panel, next to Styles or behind the » button when the panel is narrow. It shows the selected element’s name, its role and states such as Pressed.
  • A toggle button needs aria-pressed. It comes from ARIA, short for Accessible Rich Internet Applications, a set of attributes that tell assistive technology, like a screen reader, about an element without changing what it does. Without aria-pressed, the Accessibility tab showed the Show only today button’s role, button, and its name, Show only today. Nothing said whether it was on or off.
  • Keep it in step in render(), and start it in the HTML. At the end of render(), one setAttribute() line writes "true" or "false" onto aria-pressed, from todayOnly. render() runs only after an add or a press, so aria-pressed="false" in the HTML covers the page load.
  • Show the state to sighted users too. If every item is for today, the list looks the same either way. An attribute selector, [aria-pressed="true"], gives the pressed button a blue background.
  • Never remove the focus outline. outline: none looks like a style choice, but it hides the focus ring, so a keyboard user can’t see where they are. Style :focus-visible instead. The browser shows it when you move with the keyboard, not when you click a button.
  • A <div> with a click listener isn’t a button. A mouse can click it, but Tab skips it. Its role is generic, with no name. Adding role="button" changes only what a screen reader is told. Even if the div could take the focus, Enter and Space wouldn’t click it. The browser turns those keys into a click only on a real button. Use <button type="button">, and keep a class for the look.
  • A placeholder isn’t a label. The gray hint disappears as soon as someone types. A screen reader still gets the name, but nothing on screen says what the box is for. A <label> stays visible, a click on its words focuses the box, and it names the box.

Use this checklist for every page you build:

  1. Test with the keyboard alone.
  2. Use a <button> for an action and a <label> for every box.
  3. Style :focus-visible, never remove the outline.
  4. Give a toggle aria-pressed that follows your state.

In kitchen/index.html, the fixed snippet sits right after the Add the usual milk button, and the toggle goes between the message line and the list:

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

00:00 Some people can’t use a mouse. Many people using a screen reader, software that reads the page aloud, work from the keyboard too. So test Sam’s list with the keyboard alone: press Tab.

00:12 Each press moves the focus ring, the outline around the control your next key goes to, through the form in order. The Priority group is one stop, and arrow keys pick inside it.

00:24 Type eggs and three, tick boxes with Space, and press Enter on Add. The eggs go on the list. Every control works because it’s a native element: a real input, select or button with a label, which the browser handles.

00:42 Sam wants a Show only today button, like Priya’s Still to come button. A setting starts off. While it’s on, render skips items that aren’t for today. The listener flips it and redraws.

00:55 Add these lines and save, which empties the list, so add the eggs again, and the milk. Tab to the toggle and press Space, and the list drops to the eggs. Press Enter, and the milk comes back.

01:09 A sighted user sees the list change. A screen reader user doesn’t. Right-click the button, choose Inspect, and open the Accessibility tab, next to Styles or behind the double arrow.

01:20 It shows what a screen reader is told: a role, button, and a name, Show only today. Nothing says whether it’s on or off.

01:31 There’s an attribute for that state, aria-pressed. It comes from ARIA, attributes that tell assistive technology, like a screen reader, about an element. They don’t change what it does.

01:43 At the end of render, one line keeps it in step with the setting. It writes true or false onto aria-pressed, from todayOnly. Render runs only after an add or a press, so start the button with aria-pressed=”false” in the HTML.

02:01 If every item is for today, the list looks the same either way, so sighted users need the state too. An attribute selector for [aria-pressed=”true”] gives the button a blue background.

02:13 Press Space. The tab shows Pressed: true, the button turns blue, and only the eggs stay. Press Enter, and it’s false again.

02:24 Sam also pasted a snippet from an old site: a note box and a Clear list control, with its own CSS.

02:32 Click Item and press Tab. The focus moves to How many, but no ring. The snippet’s CSS sets outline: none on every focused element. That’s no cosmetic choice: a keyboard user can’t see where they are.

02:48 Replace it with a :focus-visible rule, a thick blue outline. The browser shows it when you move with the keyboard, not when you click a button. Tab, and the ring is back.

03:01 Tab from the note box, and the ring jumps to Show only today. Clear list is a div with a click listener. A mouse can click it, but Tab skips it. Its role is generic, with no name.

03:14 Adding role=”button” changes only what a screen reader is told. Even if it could take the focus, Enter and Space wouldn’t click it. The browser turns those keys into a click only on a real button.

03:27 Make it a button with type=”button”, keeping its class for the look. Tab stops on Clear list, its role is button, and Enter empties the list.

03:38 The note box’s only name is its placeholder, the gray hint inside it. Type Kofi: ripe bananas, and the hint is gone. A screen reader still gets the name, but nothing on screen says what the box is for.

03:52 Wrap it in a label, like Sam’s other boxes. The words stay visible, a click on them focuses the box, and they name it.

04:02 Here’s the checklist for every page. Test with the keyboard alone. Use a button for an action and a label for every box. Style :focus-visible, never remove the outline.

04:14 Give a toggle aria-pressed that follows your state.

04:18 Sam’s list now works from the keyboard and says when its toggle is on. Next, in Exercise: Fix the Meal Planner Controls, you’ll get Sam’s meal planner ready for the whole family.

Become a Member to join the conversation.