Alert Dialogs
Confirmation dialogs that require user acknowledgment before proceeding.
Overview
Alert dialogs are dialogs whose message requires acknowledgment. role="alertdialog" identifies that pattern, but it does not itself create assertive live-region behavior, focus containment, or modal background handling. This example combines the role with native showModal() and deliberately focuses the safe action. Choosing the least destructive action is defensive design guidance, not WCAG 3.2.1 text.
WCAG Criteria:
- 4.1.2 Name, Role, Value — the dialog must expose its role, name, and relevant states
- 3.2.1 On Focus — receiving focus must not itself cause a change of context
Key requirements:
- Use
role="alertdialog"only for a message that requires acknowledgment; do not claim deterministic urgency or speech - Label the dialog with
aria-labelledbypointing to its heading - Use
aria-describedbyfor a short plain-text warning; omit it when content is long or structured - Focus the least destructive action (e.g., Cancel, not Delete) when the dialog opens
- Trap focus within the dialog — Tab must not escape to background content
- Provide at least two actions: a safe option (Cancel) and the destructive option (Delete/Confirm)
- Use alert dialogs sparingly — overuse desensitizes users and causes fatigue
Confirmation Alert Dialog
Unsafe Popup vs. Accessible Alert Dialog
Inaccessible
View inaccessible code
<!-- Div popup — no alertdialog role, focus on destructive action -->
<button onclick="
document.getElementById('overlay').style.display = 'flex';
document.getElementById('delete-btn').focus();
">Delete Item</button>
<div id="overlay" style="
display: none; position: fixed; inset: 0;
background: rgba(0,0,0,0.5);
">
<div>
<h2>Delete this item?</h2>
<p>This action is permanent and cannot be undone.</p>
<button onclick="close()">Cancel</button>
<button id="delete-btn" onclick="close()">Delete</button>
</div>
</div>Accessible
View accessible code
<!-- Native dialog with role="alertdialog" — focus on safe action -->
<button id="trigger" onclick="
var d = document.getElementById('dlg');
d.showModal();
document.getElementById('cancel-btn').focus();
">Delete Item</button>
<dialog id="dlg"
role="alertdialog"
aria-labelledby="dlg-title"
aria-describedby="dlg-desc"
onclose="document.getElementById('trigger').focus()">
<h2 id="dlg-title">Delete this item?</h2>
<p id="dlg-desc">
This action is permanent and cannot be undone.
</p>
<button id="cancel-btn"
onclick="this.closest('dialog').close()">
Cancel
</button>
<button onclick="this.closest('dialog').close()">
Delete
</button>
</dialog>What’s wrong with the unsafe popup?
- No
alertdialogrole — screen readers announce it as generic content, not as an urgent confirmation dialog - Focus is placed on the “Delete” button (the destructive action) — a quick Enter press causes data loss
- No focus trap — Tab key moves to elements behind the overlay, so keyboard users can interact with background content
- No Escape key handling — keyboard users cannot dismiss the popup
- No
aria-labelledbyoraria-describedby— the heading and warning text are not programmatically associated with the dialog - Background content remains interactive — nothing prevents clicks or focus behind the overlay
What role="alertdialog" provides:
- A programmatic alert-dialog role for a message that requires acknowledgment
- A role distinct from the non-interactive
alertstatus type - No focus trap or live-region guarantee by itself; modal behavior here comes from native
showModal()
Expected semantics:
| Version | Programmatic result on open |
|---|---|
| Inaccessible popup | Focus lands on Delete without a dialog role, name, or associated warning. |
| Alert dialog | A named alert dialog exposes its short description and focus begins on Cancel. Exact speech varies by AT/browser. |
Why focus the safe option:
- Users may press Enter immediately after a dialog opens — if the destructive button has focus, this causes accidental data loss
- Focusing Cancel (the least destructive action) ensures that an accidental Enter press is harmless
- This is a core principle of defensive UI design for confirmation dialogs
- This protects against accidental confirmation; WCAG 3.2.1 separately requires that focus itself not trigger a context change
Alert dialog vs. standard dialog:
| Feature | dialog | alertdialog |
|---|---|---|
| Role | dialog | alertdialog |
| Meaning | General dialog | Message requiring acknowledgment |
| Use case | Forms, settings, content | Confirmations, destructive actions, warnings |
| User can ignore? | Yes (close and continue) | No — requires explicit acknowledgment |
| Frequency | Use freely | Use sparingly — overuse causes fatigue |
Key teaching points:
- Use
alertdialogfor messages requiring acknowledgment, not as a way to force an assertive announcement - Prefer the safe option for destructive confirmations; initial focus can differ when task context justifies it
- Alert dialogs should be rare — overuse desensitizes users and trains them to dismiss without reading
- The native
<dialog>element with.showModal()provides focus trapping and Escape key handling for free