Testing aria-pressed

aria-pressed indicates the current state of a toggle button. It supports true, false and mixed. It can be used on a native <button> or an element with role="button".

This page uses a set of server controls to demonstrate all three states, including how mixed can represent a group of controls with different states.

Example: servers

This example has four toggle buttons. Each button keeps the same name, such as "Server 1", whatever its state. The state is exposed to assistive technology with aria-pressed, and shown visually with a small "On", "Off" or "Mixed" badge.

The "All servers" button controls the three individual server buttons. Its aria-pressed state changes automatically based on their states:

Activating "All servers" when it is off or mixed turns every server on. Activating it when it is on turns every server off.

There is no aria-live region. Screen readers may announce changes to a button's state while it has focus, or when focus returns to it.

Try this:

  1. Turn on a screen reader.
  2. Tab to "All servers".
    It should announce as something like "All servers, toggle button, not pressed". VoiceOver doesn't announce a state when a toggle button is not pressed.
  3. Tab to "Server 2".
    It should announce as something like "Server 2, toggle button, not pressed". VoiceOver doesn't announce a state.
  4. Activate "Server 2" with Space or Enter.
    It should announce as something like "Server 2, toggle button, pressed". VoiceOver says "selected".
  5. Use Shift + Tab to return to "All servers".
    It should announce as something like "All servers, toggle button, mixed".
  6. Activate "All servers".
    Every server should turn on, and it should announce as "pressed". VoiceOver says "selected".
  7. Activate "All servers" again.
    Every server should turn off, and it should announce as "not pressed". VoiceOver doesn't announce a state.

Throughout, check that each button's name stays the same, and only its state changes.

HTML markup
<button type="button" id="all-servers" aria-pressed="false">
  All servers
  <span class="state-badge" aria-hidden="true">Off</span>
</button>
<button type="button" class="server-button" aria-pressed="false">
  Server 1
  <span class="state-badge" aria-hidden="true">Off</span>
</button>
<!-- Server 2 and Server 3 follow the same pattern -->
JavaScript
const allServers = document.getElementById('all-servers');
const serverButtons = Array.from(document.querySelectorAll('.server-button'));

// Visible badge text for each aria-pressed value
const badgeText = { 'true': 'On', 'false': 'Off', 'mixed': 'Mixed' };

// Set the state in one place, so aria-pressed and the badge never disagree.
// The button's name is never changed.
function setState(button, state) {
  button.setAttribute('aria-pressed', state);
  button.querySelector('.state-badge').textContent = badgeText[state];
}

function updateAllServers() {
  const states = serverButtons.map(btn => btn.getAttribute('aria-pressed'));

  if (states.every(state => state === 'true')) {
    setState(allServers, 'true');
  } else if (states.every(state => state === 'false')) {
    setState(allServers, 'false');
  } else {
    setState(allServers, 'mixed');
  }
}

serverButtons.forEach(btn => {
  btn.addEventListener('click', () => {
    const isPressed = btn.getAttribute('aria-pressed') === 'true';
    setState(btn, String(!isPressed));
    updateAllServers();
  });
});

allServers.addEventListener('click', () => {
  // From off or mixed, turn everything on.
  // From on, turn everything off.
  const turnOn = allServers.getAttribute('aria-pressed') !== 'true';

  serverButtons.forEach(btn => setState(btn, String(turnOn)));
  updateAllServers();
});

Design notes