Building Accessible Filter Controls for a Static Directory
Filters are the busiest controls on a local directory. Someone lands on a city page with forty plumbers and narrows it down to the ones open on weekends that offer emergency service. On a static site those filters are us
Filters are the busiest controls on a local directory. Someone lands on a city page with forty plumbers and narrows it down to the ones open on weekends that offer emergency service. On a static site those filters are usually a handful of checkboxes and a small script that hides cards that do not match. They are also one of the places where directory sites most often break for keyboard and screen reader users.
The fixes are not complicated, and most of them come from using plain HTML elements the way they were designed. This post walks through the markup, the script and the testing for filter controls on a static directory page.
Use native inputs, grouped with fieldset
Custom toggle chips built from styled divs look nice and are a pain to make accessible. You have to add roles, states, keyboard handling and focus styles by hand, and something usually gets missed. A native checkbox gives you all of that for free, and you can still style its label to look like a chip.
Group related filters in a fieldset with a legend. Screen readers announce the legend when focus enters the group, so a user hears "Hours, group" before "Open weekends, checkbox, not checked". Without it they hear a list of disconnected options with no context. Give each checkbox a visible label element tied to it, and make the label the click target so the hit area is big enough on a phone.
Announce the result count
When a sighted user ticks a filter, they see the list shrink. A screen reader user hears nothing unless you tell them. The standard approach is a status region that updates with the number of matching listings. Mark it with role="status", which behaves as a polite live region, so the announcement waits until the user finishes what they are doing.
<fieldset>
<legend>Hours</legend>
<input type="checkbox" id="f-weekend" name="weekend" value="1">
<label for="f-weekend">Open weekends</label>
<input type="checkbox" id="f-late" name="late" value="1">
<label for="f-late">Open after 6 pm</label>
</fieldset>
<p id="result-count" role="status">40 plumbers shown</p>
<script>
const cards = [...document.querySelectorAll('.listing')];
const count = document.getElementById('result-count');
document.querySelectorAll('fieldset input').forEach(box =>
box.addEventListener('change', () => {
const on = [...document.querySelectorAll('fieldset input:checked')].map(b => b.name);
let shown = 0;
cards.forEach(card => {
const ok = on.every(f => card.dataset[f] === '1');
card.hidden = !ok;
if (ok) shown++;
});
count.textContent = shown + ' plumbers shown';
})
);
</script>
Each listing card carries data attributes such as data-weekend="1" set at build time from your Google Maps export. Using the hidden attribute removes non-matching cards from the accessibility tree as well as from view, which a CSS opacity trick would not do.
Handle the empty state
Sooner or later a combination of filters returns nothing. Do not leave the user with a blank list. Show a short message in the same status region, such as "No plumbers match these filters. Try removing one.", and add a clear-filters button right below it. Make sure the button is reachable with the Tab key and that focus does not jump somewhere unexpected after it runs. Moving focus back to the first checkbox in the group is a reasonable choice.
Keep it working without JavaScript
Static directories have an advantage here. The full list is already in the HTML, so with scripts off a visitor still sees every listing and can scroll. That is a fine fallback. What you want to avoid is filters that look clickable but do nothing when the script fails. One simple fix is to render the filter fieldset with the hidden attribute and remove it from the script once it loads, so the controls only appear when they will work.
These notes on layout and usability for directory websites cover more of the design side, and the planning behind which filters exist at all is covered in this article on planning categories and filters for a new local directory. Fewer, well-chosen filters are easier to make accessible than a long row of options nobody uses.
Style focus and checked states clearly
Do not remove the focus outline. If the default outline clashes with your design, replace it with a clearer one using :focus-visible, which shows the ring for keyboard users without showing it on every mouse click. The checked state should differ by more than color, for example with a check mark or a filled background plus a border, so it reads for users who cannot tell your two brand colors apart.
Keep tap targets comfortable. WCAG 2.2 added a minimum target size criterion at level AA, and chip-style labels with some padding clear it easily.
Test with a keyboard and a screen reader
Run through the filters with only the keyboard: Tab into the group, Space to toggle, Tab to the results. Then try a screen reader. VoiceOver on macOS and iOS and NVDA on Windows are free and cover most of what you need to hear. Listen for the legend, the checkbox state and the count update. Automated checkers like axe catch missing labels, but they cannot tell you whether the experience makes sense.
Questions about accessible directory filters
Should filters update the URL?
It helps. Writing the active filters to the query string lets people share or bookmark a filtered view. Read the query string on load and set the checkboxes to match, then run the same filter function.
Are select dropdowns better than checkboxes for filters?
For a single choice from a long list, such as a county, a native select works well. For several independent yes or no options, checkboxes are clearer and easier to scan.
Does the directory builder handle this for me?
1 Click Directory Builder turns a Google Maps export from Outscraper, Apify or any scraper into a complete static directory with search, filters and a map. The patterns above are worth checking on any directory you ship, whichever tool built it.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.