Skip to curriculum
Jump to the editor

26.3. Announce changes with a live region

Easy
Topics

This question is in

26. Accessibility
15–20 min

A sighted user sees "3 results", "Saved" or "Payment failed" appear on the page. A screen reader user hears nothing unless the page tells the screen reader. The tool is a live region: an element with aria-live, whose text changes are read out. It is easy to get wrong. A region created at the moment of the message is often not announced, display: none hides it from screen readers too, and writing the same text twice is not a change, so the second "Item added" is silent.

Write createAnnouncer() in announcer.js.

Signature

JAVASCRIPT
function createAnnouncer() // returns { announce, clear, destroy }

announcer.announce(message, politeness)  // politeness: "polite" (default) or "assertive"
announcer.clear()                        // empties both regions, cancels pending messages
announcer.destroy()                      // removes the regions

Requirements

  1. createAnnouncer() adds two empty regions to document.body straight away: one with aria-live="polite" and role="status", one with aria-live="assertive" and role="alert", both with aria-atomic="true".
  2. The regions are visually hidden but not hidden from assistive technology: not display: none, not visibility: hidden, no hidden or aria-hidden; position: absolute and at most 1 by 1 pixel.
  3. announce(message) empties the polite region at once and writes the message into it 100 ms later. announce(message, "assertive") does the same in the assertive region.
  4. Announcing the same text again after it was written is a new change: the region is emptied first, then written again 100 ms later.
  5. A newer message for a region replaces one that is still waiting, even when it is the same text: two calls within 100 ms give one write. Each region has its own pending message.
  6. The message is written as text, never as HTML, after String(). An empty message only clears the region and schedules nothing.
  7. Any politeness other than "polite" or "assertive" throws RangeError (but see rule 9).
  8. clear() empties both regions and cancels pending messages.
  9. destroy() removes both regions and cancels every timer. After it, every method does nothing and does not throw, whatever its arguments (an unknown politeness included).
  10. Two announcers are independent.

Examples

JAVASCRIPT
const announcer = createAnnouncer();
searchBox.addEventListener("input", debounce(async () => {
  const results = await search(searchBox.value);
  render(results);
  announcer.announce(`${results.length} results`);   // read after the current speech
}, 300));
JAVASCRIPT
addButton.addEventListener("click", () => {
  cart.add(item);
  announcer.announce("Item added to cart");
});
// Click, then click again a second later: the region is emptied and written
// again, so a screen reader should read the message a second time.
// Two calls within 100 ms are one write: the second replaces the first.
JAVASCRIPT
announcer.announce("Card declined", "assertive");   // meant to interrupt current speech
announcer.announce("x", "loud");                    // RangeError
announcer.destroy();
announcer.announce("x", "loud");                    // does nothing after destroy

Environment

  • Plain JavaScript in a browser page; the tests call the global createAnnouncer from announcer.js.
  • Timers run on a virtual clock that replaces setTimeout and clearTimeout, and counts waiting timers.
  • The tests check the regions' attributes, computed styles, size and text. They cannot hear a screen reader: what is spoken, and when, is the intended result, and depends on the screen reader and browser. Check it by hand with one (for example: add an item twice, a second apart, and listen for the message both times).
  • Out of scope: queueing several messages so each is read in full, and clearing messages after a while.

Hints

  1. Why would a region that is created and filled in the same moment be missed?
  2. Which CSS hides an element from the eye but keeps it in the accessibility tree?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…