Sub topic 14
Making Search and Filters Accessible
This chapter assumes you can write basic HTML forms and TypeScript, and that you know what a screen reader is. You do not need to be an ARIA expert. ARIA (Accessible Rich Internet Applications) is a set of HTML attributes that tell assistive technology what custom widgets are and what state they are in.
Why search and filters need extra care
Search and filter interfaces change the page without a navigation. A user types, and a list appears. A user ticks a box, and the results shrink. Sighted mouse users see this instantly. Three groups of people can miss it:
- Screen reader users hear nothing unless you announce the change.
- Keyboard users cannot reach custom widgets that only respond to clicks.
- Low-vision users who zoom or reflow the page can lose controls off-screen.
The rule that covers most cases: every state change that a sighted user can see must also be exposed in text, in focus order, or in an announcement.
Label the search field and mark the landmark
A landmark is a page region that assistive technology can jump to directly. Wrapping the search form in role="search" gives users a shortcut to it. Give the input a visible <label>. A placeholder is not a label. It disappears when the user types, it often has low contrast, and some screen readers skip it.
<form role="search" action="/search" method="get">
<label for="q">Search products</label>
<input id="q" name="q" type="search" autocomplete="off">
<button type="submit">Search</button>
</form>
<p role="status">24 results for "boots"</p>If a design hides the label visually, hide it with a "visually hidden" CSS class that keeps it readable by screen readers. Do not use display: none, because that removes it from the accessibility tree. Always keep a real submit button. Pressing Enter should work, but a visible button helps people using voice control ("click Search") and switch devices.
Do not put autocomplete="on" hints such as name or email on a search field. The autocomplete="off" above stops the browser's own history dropdown from covering your suggestions.
Announce results with a live region
A live region is an element whose text changes are read aloud automatically. The role="status" element above is a polite live region: the screen reader waits until it finishes the current sentence, then reads the update. WCAG success criterion 4.1.3 (Status Messages) requires this kind of announcement for result counts.
Two common errors:
- Creating the region at the same moment as the message. Many screen readers only watch regions that already existed in the page. Render an empty
role="status"element on load, then change its text. - Announcing too often. If you update the region on every keystroke, the user hears a stream of numbers. Announce after results settle.
function announce(message: string): void {
const region = document.getElementById("result-status");
if (region) region.textContent = message;
}
function describeResults(count: number, query: string): string {
if (count === 0) return `No results for "${query}"`;
return `${count} result${count === 1 ? "" : "s"} for "${query}"`;
}
announce(describeResults(24, "boots"));Use role="alert" (assertive) only for errors that need immediate attention, such as "Search failed. Try again." Assertive messages interrupt the user's current reading.
Autocomplete as a combobox
A suggestion list is a combobox: a text input that controls a popup list. The ARIA 1.2 pattern keeps keyboard focus in the input and uses aria-activedescendant to point at the highlighted option. The screen reader reads that option even though focus never leaves the field, so the user can keep typing.
| Attribute | Placed on | Purpose |
|---|---|---|
| `role="combobox"` | input | Declares the widget type |
| `aria-expanded` | input | `true` when the list is open |
| `aria-controls` | input | Points to the list's `id` |
| `aria-autocomplete="list"` | input | Says suggestions appear in a list |
| `aria-activedescendant` | input | `id` of the highlighted option |
| `role="listbox"` / `role="option"` | list / items | Marks the popup and its choices |
| `aria-selected` | option | `true` for the highlighted option |
<label for="q">Search products</label>
<input id="q" type="search" role="combobox" autocomplete="off"
aria-expanded="false" aria-controls="q-list" aria-autocomplete="list">
<ul id="q-list" role="listbox" aria-label="Suggestions" hidden>
<li id="opt-1" role="option" aria-selected="false">boots</li>
<li id="opt-2" role="option" aria-selected="false">boot socks</li>
</ul>function setupCombobox(input: HTMLInputElement, list: HTMLElement): void {
let active = -1;
const options = () => Array.from(list.querySelectorAll<HTMLElement>('[role="option"]'));
function setActive(index: number): void {
const items = options();
items.forEach((el, i) => el.setAttribute("aria-selected", String(i === index)));
active = index;
if (index >= 0) input.setAttribute("aria-activedescendant", items[index].id);
else input.removeAttribute("aria-activedescendant");
}
function open(): void {
list.hidden = false;
input.setAttribute("aria-expanded", "true");
}
function close(): void {
list.hidden = true;
input.setAttribute("aria-expanded", "false");
setActive(-1);
}
input.addEventListener("keydown", (e) => {
const items = options();
if (e.key === "ArrowDown" && items.length > 0) {
e.preventDefault();
open();
setActive((active + 1) % items.length);
} else if (e.key === "ArrowUp" && items.length > 0) {
e.preventDefault();
open();
setActive(active <= 0 ? items.length - 1 : active - 1);
} else if (e.key === "Enter" && active >= 0) {
e.preventDefault();
input.value = items[active].textContent ?? "";
close();
input.form?.requestSubmit();
} else if (e.key === "Escape" && !list.hidden) {
e.preventDefault();
close();
}
});
// Keep the click from blurring the input before it registers.
list.addEventListener("mousedown", (e) => e.preventDefault());
list.addEventListener("click", (e) => {
const target = (e.target as HTMLElement).closest<HTMLElement>('[role="option"]');
if (!target) return;
input.value = target.textContent ?? "";
close();
input.form?.requestSubmit();
});
input.addEventListener("blur", close);
}
const input = document.getElementById("q") as HTMLInputElement;
const list = document.getElementById("q-list") as HTMLElement;
setupCombobox(input, list);Also announce how many suggestions are available (for example "5 suggestions") in a live region when the list opens. Screen reader users otherwise cannot tell an empty list from a closed one.
Filter controls: use native elements first
Native form controls come with keyboard support, focus handling, and screen reader semantics. Rebuilding them from div elements means you must rebuild all of that.
- Multiple-choice facets: checkboxes inside a
<fieldset>with a<legend>. The legend is read as the group name. - Single-choice options: radio buttons in a fieldset.
- Numeric ranges: two labeled number inputs, or
<input type="range">with a visible value. Custom dual-handle sliders needrole="slider", arrow-key support, and a text alternative, so offer number fields as a fallback. - Show/hide panels: a
<button>witharia-expandedandaria-controls.
<fieldset>
<legend>Category</legend>
<label><input type="checkbox" name="category" value="boots"> Boots (12)</label>
<label><input type="checkbox" name="category" value="sandals"> Sandals (7)</label>
</fieldset>Put the facet count inside the label text. The user then hears "Boots, 12, checkbox, not checked" in one go. A count in a separate span placed far from the input may be read out of context.
Do not communicate state by colour alone. A selected facet needs a check mark or other shape, with at least 3:1 contrast against its background (WCAG 1.4.11, Non-text Contrast).
Applied filters and reset
Applied filters, often shown as removable "chips", are buttons. Give each one a name that says what it does, and group them in a labeled list.
<ul aria-label="Applied filters">
<li>
<button type="button" aria-label="Remove filter: Category Boots">
Boots <span aria-hidden="true">×</span>
</button>
</li>
</ul>
<button type="button">Clear all filters</button>The visible "×" is hidden from assistive technology because the aria-label already says "Remove". The accessible name still contains the visible text "Boots", so voice control users can say "click Boots" and it works (WCAG 2.5.3, Label in Name).
When a user removes a chip, the chip disappears, and focus would be lost to the top of the page. Move focus to the next chip, or to the "Clear all" button or the results heading if none remain. Then announce the new result count.
Focus management after results change
Do not move focus when a filter checkbox is toggled. The user is probably about to tick another box, and stealing focus breaks that. Leave focus on the control and update the live region.
Move focus only when the user performs a deliberate submit action:
- After submitting a search, move focus to the results heading (give it
tabindex="-1"), or let a full page load do it. - On mobile, if filters live in a dialog, use the native
<dialog>element withshowModal(). It moves focus inside, traps it, closes on Escape, and returns focus to the opening button. Label the dialog and give "Show 24 results" as the apply button text.
Layout, targets, and zoom
- Visible focus: never remove the focus outline without a replacement that has at least 3:1 contrast (WCAG 2.4.7 and 2.4.11).
- Target size: make touch targets at least 24 by 24 CSS pixels (WCAG 2.5.8). 44 pixels is more comfortable, especially for chips and the remove buttons inside them.
- Reflow: at 320 CSS pixels wide (400% zoom on a desktop), the search field and filters must work without two-dimensional scrolling (WCAG 1.4.10). A sticky filter sidebar that covers half the screen at high zoom is a failure.
- Reduced motion: respect
prefers-reduced-motionfor animated list updates and panel slides.
Common errors
| Problem | Effect | Fix |
|---|---|---|
| Placeholder used as the only label | No name once the user types | Add a visible `<label>` |
| Live region added with its message | Message never read | Render an empty region on load |
| Filters built from clickable `div` elements | Not reachable by keyboard | Use checkboxes, radios, or buttons |
| Focus lost after chip removal | Keyboard user restarts at top | Move focus deliberately |
| Count announced on every keystroke | Speech floods the user | Debounce, then announce once |
| Only colour shows the selected state | Invisible to colour-blind users | Add a check mark or text |
Combine debouncing from the latency chapter with announcements. The delay that prevents a request per keystroke also prevents an announcement per keystroke.
Test checklist
- Unplug the mouse. Can you search, open suggestions, pick one, apply two filters, remove one, and clear all?
- Turn on a screen reader. Is every control named, and is the result count announced once after each change?
- Zoom to 400%. Does everything remain reachable?
- Check focus after every action. Is it always somewhere visible and logical?
Exercise
A user ticks the "Boots" checkbox in a filter group. The results list updates, but a screen reader user hears nothing. The page has a <p id="result-status"> that you create with JavaScript at the moment the results arrive, and you set its text at the same time. Name two changes that make the announcement reliable, and say where focus should be afterwards.
Show answer
First, put the status element in the HTML on page load, empty, with role="status", and later change only its text. Many screen readers do not announce a region that is created together with its content. Second, debounce the update and announce one message after the results settle, for example "12 results for boots". Focus should stay on the "Boots" checkbox so the user can continue ticking other filters.