Web basics · Easy · 8 min

An accessible toggle button

A button that switches on and off must say so to screen readers too, not just with colour.

The #mute button in the preview silences notifications. Make it a toggle: each click switches aria-pressed between "false" and "true" and its text between Mute and Unmute.

Start with aria-pressed="false". Keep it a <button> (it already is): that way the keyboard (Enter and Space) works with no extra code.

Challenges 0/3

  • Starts with aria-pressed="false"
  • One click switches it on and changes the label
  • A second click returns it to the start

const btn = document.getElementById('mute');

btn.addEventListener('click', () => {
  // toggle aria-pressed and the label
});

Tab indents, Ctrl+Z undoes. To leave the editor with the keyboard: Esc, then Tab.

Console output appears here (console.log).

Why this matters

AI interfaces are full of switches: local or cloud mode, streaming, microphone, use my documents. If the state only shows in the colour, someone using a screen reader cannot tell whether their text is about to leave the machine, and that stops being a design detail and becomes a privacy problem. aria-pressed and a label that changes solve it with two attributes. It is the same pattern as the My Ollama switch in these exercises. More interface pieces around a model are on the developers page.

Go deeper: the W3C WAI reference →

This in production, with your data? Let's talk for 15 minutes →