Combobox (Autocomplete)
A combobox combines a text input with a dynamic listbox of filtered options (e.g., location fields, tag pickers, product search inputs).
Native HTML <input list="…"> elements provide poor styling control across modern browsers and behave inconsistently across operating systems. Building a robust combobox requires adhering to the WAI-ARIA combobox / listbox specification.
Live Demo
Type a query into the input below (e.g., "Os"), then cycle through recommendations using the Up and Down arrow keys (↑ / ↓) and select via Enter:
Keyboard Interaction
| Key Interaction | Expected Behavioral Contract |
|---|---|
| Typing Characters | Filters candidate options in real time; focus strictly remains on the input field |
↓ / ↑ | Traverse highlighted items (virtual focus via aria-activedescendant) |
Enter | Commit the highlighted option as the input value and close the popup listbox |
Esc | Dismiss the listbox without mutating the input text |
Pointer Interactions
| Pointer Action | Expected Behavioral Contract |
|---|---|
| Click Option | Selects the target option and closes the listbox (matching keyboard Enter selection) |
| Click Outside | Dismisses the listbox without mutating the current input value |
Structure
Rules
- Give the input the combobox attributes and switch them as the options show and hide MUSTUI-R32
- Add
role="combobox",aria-autocomplete="list"and anaria-controlsthat points to the listbox, and switcharia-expandedwith the list. Screen readers announce that typing brings up options. - Check:
CBX-01CBX-01Combobox attributes✕ NGStill false with options shown✓ OKTrue while options are shown
- Keep focus in the input while moving through options, and point at the option with
aria-activedescendantMUSTUI-R33 - As
↑↓move through the options, DOM focus stays in the input and the highlighted option'sidgoes intoaria-activedescendant. Users can keep typing while choosing. - Check:
CBX-02CBX-02Focus while moving options✕ NGFocus leaves the input✓ OKFocus stays in the input
- Confirm with
Enter; close withEscwithout changing the value MUSTUI-R34 - With confirming and cancelling on separate keys, no value gets confirmed by accident.
- Check:
CBX-03CBX-03Enter and Esc✕ NGEsc fills in the option✓ OKInput value is unchanged
- Announce the number of options in a status message each time the list is filtered MUSTUI-R35
- Put text such as "3 options available" in a hidden
role="status"(oraria-live="polite") region. People who cannot see the list still know whether there are options. - Check:
CBX-04CBX-04Announcing the count✕ NGNothing is announced✓ OKThe count is announced
- Close without confirming when users click outside the input and the list MUSTUI-R36
- An option that was only highlighted is not entered by accident.
- Check:
CBX-05CBX-05Click outside✕ NGOutside click fills it in✓ OKCloses without committing
Implementation
A complete, production-grade reference module:
View Source Code (HTML Markup and JavaScript Module)
html
<label for="city" id="city-label">City</label>
<input
type="text"
id="city"
role="combobox"
aria-expanded="false"
aria-controls="city-listbox"
aria-autocomplete="list"
autocomplete="off"
/>
<ul id="city-listbox" role="listbox" aria-labelledby="city-label" hidden>
<li role="option" id="opt-osaka">Osaka</li>
<li role="option" id="opt-okinawa">Okinawa</li>
<li role="option" id="opt-okayama">Okayama</li>
</ul>
<div class="visually-hidden" role="status" id="city-status"></div>javascript
// modules/combobox.js
export function initCombobox(input, listbox, status) {
let activeIndex = -1
function allOptions() {
return [...listbox.querySelectorAll('[role="option"]')]
}
function filter(value) {
const query = value.trim().toLowerCase()
allOptions().forEach((item) => {
item.hidden = query.length > 0 && !item.textContent.toLowerCase().includes(query)
})
const visible = allOptions().filter((item) => !item.hidden)
status.textContent = `${visible.length} results available`
if (visible.length > 0) {
listbox.hidden = false
input.setAttribute('aria-expanded', 'true')
} else {
listbox.hidden = true
input.setAttribute('aria-expanded', 'false')
}
}
function setActive(index) {
const visible = allOptions().filter((item) => !item.hidden)
if (index < 0 || index >= visible.length) {
activeIndex = -1
input.removeAttribute('aria-activedescendant')
allOptions().forEach((opt) => opt.classList.remove('is-active'))
return
}
activeIndex = index
allOptions().forEach((opt) => opt.classList.remove('is-active'))
const activeItem = visible[activeIndex]
activeItem.classList.add('is-active')
input.setAttribute('aria-activedescendant', activeItem.id)
}
input.addEventListener('input', (e) => {
filter(e.target.value)
setActive(-1)
})
input.addEventListener('keydown', (e) => {
const visible = allOptions().filter((item) => !item.hidden)
if (e.key === 'ArrowDown') {
e.preventDefault()
if (listbox.hidden) {
filter(input.value)
}
setActive(activeIndex < visible.length - 1 ? activeIndex + 1 : 0)
} else if (e.key === 'ArrowUp') {
e.preventDefault()
setActive(activeIndex > 0 ? activeIndex - 1 : visible.length - 1)
} else if (e.key === 'Enter') {
if (activeIndex >= 0 && visible[activeIndex]) {
e.preventDefault()
input.value = visible[activeIndex].textContent.trim()
listbox.hidden = true
input.setAttribute('aria-expanded', 'false')
}
} else if (e.key === 'Escape') {
listbox.hidden = true
input.setAttribute('aria-expanded', 'false')
}
})
document.addEventListener('click', (e) => {
if (!input.contains(e.target) && !listbox.contains(e.target)) {
listbox.hidden = true
input.setAttribute('aria-expanded', 'false')
}
})
}- Return to Keyboard & ARIA Patterns