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:
- All off:
aria-pressed="false", badge shows "Off". - All on:
aria-pressed="true", badge shows "On". - Some on, some off:
aria-pressed="mixed", badge shows "Mixed".
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:
- Turn on a screen reader.
- 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. - Tab to "Server 2".
It should announce as something like "Server 2, toggle button, not pressed". VoiceOver doesn't announce a state. - Activate "Server 2" with Space or Enter.
It should announce as something like "Server 2, toggle button, pressed". VoiceOver says "selected". - Use Shift + Tab to return to "All servers".
It should announce as something like "All servers, toggle button, mixed". - Activate "All servers".
Every server should turn on, and it should announce as "pressed". VoiceOver says "selected". - 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
- Consistent button names: Each button keeps the same name while
aria-pressedcommunicates its state. This follows the WAI-ARIA Authoring Practices recommendation for toggle buttons. - Visible states: The "On", "Off" and "Mixed" badges make the states clear without relying on colour alone (WCAG 1.4.1 Use of Colour).
- Screen reader support: The badges use
aria-hidden="true"so they are not included in the accessible names. Screen readers get the state fromaria-pressedinstead. - Mixed state: When "All servers" is mixed, activating it turns every server on. This is a design choice, not a requirement of
aria-pressed.