Skip to content

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 ​

Keyboard operations and expected behavior
Key InteractionExpected Behavioral Contract
Typing CharactersFilters candidate options in real time; focus strictly remains on the input field
↓ / ↑Traverse highlighted items (virtual focus via aria-activedescendant)
EnterCommit the highlighted option as the input value and close the popup listbox
EscDismiss the listbox without mutating the input text

Pointer Interactions ​

Pointer operations and expected behavior
Pointer ActionExpected Behavioral Contract
Click OptionSelects the target option and closes the listbox (matching keyboard Enter selection)
Click OutsideDismisses the listbox without mutating the current input value

Structure ​

Osak|role="combobox"aria-expandedOsakaOkinawaOkayamarole="listbox"aria-activedescendant→ selected option ID,DOM focus never leavesthe text input↕ moves highlight only — never shifts DOM focus from the input

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 an aria-controls that points to the listbox, and switch aria-expanded with the list. Screen readers announce that typing brings up options.
Check: CBX-01CBX-01 Combobox attributes✕ NGStill false with options shownCombobox whose aria-expanded stays false while options are shown✓ OKTrue while options are shownCombobox whose aria-expanded becomes true while options are shown
Keep focus in the input while moving through options, and point at the option with aria-activedescendant MUSTUI-R33
As ↑ ↓ move through the options, DOM focus stays in the input and the highlighted option's id goes into aria-activedescendant. Users can keep typing while choosing.
Check: CBX-02CBX-02 Focus while moving options✕ NGFocus leaves the inputCombobox where Down moves focus from the input to an option✓ OKFocus stays in the inputCombobox where focus stays in the input while only the highlight moves
Confirm with Enter; close with Esc without changing the value MUSTUI-R34
With confirming and cancelling on separate keys, no value gets confirmed by accident.
Check: CBX-03CBX-03 Enter and Esc✕ NGEsc fills in the optionOn Esc, the highlighted option is filled into the input✓ OKInput value is unchangedOn Esc, the input keeps the typed value
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" (or aria-live="polite") region. People who cannot see the list still know whether there are options.
Check: CBX-04CBX-04 Announcing the count✕ NGNothing is announcedFiltering the options announces no count✓ OKThe count is announcedFiltering the options announces the number of options
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-05 Click outside✕ NGOutside click fills it inClicking outside fills the highlighted option into the input✓ OKCloses without committingClicking outside closes the list and keeps the typed value

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')
    }
  })
}