Live region roles
Hey ,
I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.
Please upgrade (use this link) access this content.
I'm super eager to help you learn more!
Hey ,
I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.
Please upgrade (use this link) access this content.
I'm super eager to help you learn more!
Live regions are special.
When something changes on a webpage, a sighted user can see what changed. But a blind user can’t. Live regions exist to tell blind users what has changed. They announce the changes.
There are five live region roles:
alertstatuslogmarqueetimerAlert contains messages important and time-sensitive messages. One example is error messages. You should not use alert unless the messages are important AND time-sensitive.
<div role="alert"> ... </div>
Status tells the user something has changed. This contains information that’s not important to warrant an alert.
<div role="status"> ... </div>
Log is used for regions where information is added in a meaningful order. It can be used for things like chat logs or game logs. Old information may be removed from logs.
<div role="log"> ... </div>
Marquee is used for non-essential information that changes regularly. An example is a stock ticker.
<div role="marquee"> ... </div>
Timer is used for any kind of timer or clock.
<div role="timer"> ... </div>
You only need to use alert and status to make most components screen-reader accessible. There’s no need for log, marquee, or timer.
Each live region role is paired with live region attributes. Here are the pairings:
| Role | aria-live |
aria-atomic |
|---|---|---|
| alert | assertive |
true |
| status | polite |
true |
| log | polite |
false |
| marquee | off |
false |
| timer | off |
false |
aria-live and aria-atomic are live-region related aria-properties/states. I’m going to call them live-region attributes to make it easier on the ears
There are four live-region attributes:
aria-livearia-atomicaria-relevantaria-busyYou don’t need to use these attributes most of the time. (alert and status roles are enough). But I’ll still explain what each attribute does for the sake of completeness.
Aria-live tells screen readers when to mention changes. It can be set to assertive, polite, or off:
assertive: Announce changes immediatelypolite: Queue changes and announces them when the next opportunity arisesoff: Don’t announce changesIf you set aria-live to assertive, it’s almost the same as setting role to alert.
<div aria-live="assertive"> ... </div>
Aria-atomic tells screen readers what the nodes it should mention when something changes. It can be true or false.
true: Announces the entire live regionfalse: Announces only the changed node in the live region.<div aria-atomic="true"> ... </div>
Aria-relevant tells screen readers what kind of changes to mention. It is a space delimited list that can contain these four values:
additions: Only announce when things are added to the live regionremovals: Only announce when things get removed from the live regiontext: Only announce when text was changedall: Announce all changesaria-relevant defaults to additions text.
<div aria-relevant="additions text"></div>
Aria-busy tells screen readers whether the live region is still being updated. It can be set to true or false.
true: Screen readers should wait until aria-busy is set to false before announcing changes.<div aria-busy="true"></div>
In this example, you’ll see Voiceover announce the changes in the <div> with an alert role.
<div role="alert"></div>
<form>
<label for="input"> Text to announce </label>
<input type="text" id="input">
<button> Add text to live region </button>
</form>
const form = document.querySelector('form')
const input = form.querySelector('input')
const alertRegion = document.querySelector('[role="alert"]')
form.addEventListener('submit', event => {
event.preventDefault()
const text = input.value.trim()
alertRegion.textContent = text
})
You only need to know two things:
alert rolestatus roleRemember them yet? Good. You can discard the rest.
That’s it!