JavaScript Accessibility Cheat Sheet for Everyday UI Work
Build accessible JavaScript features faster with a practical checklist, patterns, and a central cheat sheet you can keep open while you code.
Why this cheat sheet exists
JavaScript is often where accessibility wins (or regressions) happen: focus moves, ARIA updates, keyboard handling and dynamic content all live here. The goal of this post is simple: keep a compact, actionable reference nearby so your UI behavior works for keyboard, screen reader and touch users.
Below is the core cheat sheet. Everything else in this post expands on it with examples and small reminders.
Accessibility cheat sheet (keep this table open)
| Scenario | JavaScript task | Accessibility cue | Snippet / reminder |
|---|---|---|---|
| Open a modal | Move focus into the modal and trap it | Focus should land on the modal title or first control | Code Code |
| Close a modal | Restore focus to the opener | Users should return to where they were | Code Code |
| Toggle a disclosure | Update aria-expanded and hidden | Screen readers rely on state changes | Code |
| Build custom buttons | Use Code | Space/Enter should activate | Code |
| Update live content | Announce new information | Use Code | Code |
| Validate a form | Link errors to inputs | Use Code Code | Code |
| Dynamic lists | Preserve selection and keyboard nav | Avoid focus loss on rerender | Keep Code |
| Skip links | Reveal on focus and scroll | Provide a fast route to main content | Code |
| Infinite scroll | Offer a “Load more” fallback | Continuous scroll can trap users | Code Code |
| Tooltip | Show on focus + hover | Keyboard users need access | Code |
| Carousel | Pause, play, and announce | Motion should be user-controlled | Add pause button + Code |
| Drag and drop | Provide keyboard alternative | Dragging alone is not enough | Offer move up/down buttons |
Key principles before you write code
- Native elements first. If you can use <button>, <details>, or <dialog>, do it. You get keyboard handling and semantics for free.
- Focus is a feature. Any time you open, close, or reorder UI, ask: Where should focus go next?
- State must be perceivable. If something expands, collapses, or changes state, reflect it in ARIA or visible text.
- Keyboard parity. Everything you can click should be operable with Tab + Enter/Space.
Patterns you can copy
Accessible disclosure component
Codeconst button = document.querySelector('[data-disclosure-button]'); const panel = document.querySelector('[data-disclosure-panel]'); button.addEventListener('click', () => { const isOpen = button.getAttribute('aria-expanded') === 'true'; button.setAttribute('aria-expanded', String(!isOpen)); panel.hidden = isOpen; });
Checklist: aria-expanded updates, panel uses hidden and the button remains a real <button> element.
Focus management for modal dialogs
Codeconst dialog = document.querySelector('[data-dialog]'); const openButton = document.querySelector('[data-dialog-open]'); const closeButton = dialog.querySelector('[data-dialog-close]'); openButton.addEventListener('click', () => { dialog.showModal(); closeButton.focus(); }); closeButton.addEventListener('click', () => { dialog.close(); openButton.focus(); });
If you cannot use <dialog>, mirror the behavior: add role="dialog", trap focus within the modal and restore focus to the opener when closed.
Live region for async status updates
Codeconst status = document.querySelector('[data-status]'); function notify(message) { status.textContent = ''; requestAnimationFrame(() => { status.textContent = message; }); }
Use a div with aria-live="polite" so screen readers announce updates without taking over.
Common pitfalls and quick fixes
- Pitfall: Adding onClick to a <div> without keyboard support.
Fix: Use a <button> or add tabindex="0", plus Enter/Space handlers. - Pitfall: Triggering a dropdown but not telling assistive tech.
Fix: Add aria-expanded to the trigger and toggle it on click. - Pitfall: Rerendering a list and losing focus.
Fix: Keep focus on the selected item and restore it after DOM updates.
How to use this in daily work
- Paste the cheat sheet into your project notes or wiki.
- Run through it when you add a new interactive component.
- Pair it with a quick keyboard pass (Tab, Shift+Tab, Enter, Space, Escape).
Final takeaway
Accessibility is not a separate project; it is a set of tiny decisions. If you can remember to manage focus, keep keyboard parity, and announce state changes, your JavaScript will already outperform most production UIs. Keep the table handy, and you will ship inclusive features faster.