Under Construction: The content on this site draft material. It is not the official content of this site and should not be followed at this time.

Not Legal Advice: The content on this site is provided for informational purposes only and does not constitute legal advice. Consult a qualified attorney for advice specific to your situation.

Purpose

A switch represents an immediate on/off state change (like enabling a feature or toggling a setting). It differs from a checkbox in that switches take effect immediately without requiring form submission.

Usage Guidance

  • Use switches for settings that take immediate effect
  • Use checkboxes for form selections that require submission
  • Label the switch clearly; the label should not change based on state
  • State (on/off) is communicated by aria-checked, not by changing the label

Keyboard Support

Key Action
Tab Move focus to the switch
Space Toggle the switch state

Screen Reader Behavior

Screen readers announce the label and “switch, on” or “switch, off.”

ARIA Requirements

  • role="switch" on the control element
  • aria-checked="true|false" reflecting the current state
  • tabindex="0" if using a non-button element (prefer <button role="switch">)

Common Mistakes

  • Changing the label text between states (makes it hard to find the same control)
  • Not updating aria-checked on state change
  • Using aria-checked without role="switch" (use role="checkbox" or role="switch")
  • Implementing a switch that requires form submission (use a checkbox instead)

Live Example


Code Example

<button
  role="switch"
  aria-checked="false"
  id="notifications-switch"
>
  <!-- Visual indicator via CSS/JS -->
</button>
<label for="notifications-switch">Email notifications</label>

<script>
  var sw = document.getElementById('notifications-switch');
  sw.addEventListener('click', function() {
    var checked = sw.getAttribute('aria-checked') === 'true';
    sw.setAttribute('aria-checked', String(!checked));
  });
  sw.addEventListener('keydown', function(e) {
    if (e.key === ' ') {
      e.preventDefault();
      sw.click();
    }
  });
</script>