HTML

ARIA Live Region for Dynamic Announcements

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Use `aria-live` on a container so screen readers announce its content changes (toast notifications, form errors, loading states). `polite` waits for a pause; `assertive` interrupts.
HTML
Raw
<!-- Polite — waits until the user is idle (chat messages, status updates) -->
<div role="status" aria-live="polite" aria-atomic="true" class="sr-only" id="status">
    <!-- JS writes here; screen reader announces -->
</div>

<!-- Assertive — interrupts immediately (errors, urgent alerts) -->
<div role="alert" aria-live="assertive" class="sr-only" id="errors">
</div>

<script>
function announce(msg, urgent = false) {
    const region = document.getElementById(urgent ? 'errors' : 'status');
    region.textContent = '';                  // reset so identical messages re-announce
    setTimeout(() => { region.textContent = msg; }, 50);
}

// Examples:
announce('Saved successfully');
announce('Could not save — check your connection', true);
</script>

<!-- aria-atomic="true" reads the WHOLE region on change, not just the diff.
     For a counter ("5 items loaded → 10 items loaded"), set atomic=true.
     For a chat log where new items append, set atomic=false. -->
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.