Autocomplete combobox with filtering, keyboard navigation, and native form association. Supports single-select and multi-select tag modes.
Overview
An autocomplete combobox following the W3C ARIA combobox pattern. Users type to filter a list of options, navigate with arrow keys, and select with Enter or click. The selected value participates in native form submission via ElementInternals.
Add multiple to enable multi-select tag mode, where users can select multiple items as tag chips.
Usage
Place an <input> and a <ul> inside <combo-box>. Each <li> needs a data-value attribute for the form value. The visible text content becomes the label. Give the input an id and point a <label for> at it: the component keeps the input’s id and labels, so the label names the combobox. A placeholder is not a label.
12345678910
<label for="country">Country</label><combo-box> <input type="text" id="country" name="country" placeholder="Search countries..."> <ul> <li data-value="us">United States</li> <li data-value="gb">United Kingdom</li> <li data-value="ca">Canada</li> <li data-value="au">Australia</li> </ul></combo-box>
Inside a Form
Put the name on the inner input. Without JavaScript the plain input submits the typed text under that name. On upgrade the component moves the name to itself (if the host has a name too, the host wins) and removes it from the input, so the form submits exactly one entry: the selected option’s data-value. Add required to enable required validation, and disabled (or a disabled ancestor <fieldset>) to disable the field.
123456789101112131415161718192021
<form id="my-form"> <label for="fruit">Fruit</label> <combo-box required> <input type="text" id="fruit" name="fruit" placeholder="Pick a fruit..."> <ul> <li data-value="apple">Apple</li> <li data-value="banana">Banana</li> <li data-value="cherry">Cherry</li> </ul> </combo-box> <button type="submit">Submit</button></form> <script> document.getElementById('my-form') .addEventListener('submit', (e) => { e.preventDefault(); const data = new FormData(e.target); console.log('fruit:', data.get('fruit')); });</script>
Filter Modes
By default, typing filters options by substring match (contains). Set filter="startsWith" to only match from the beginning of each option.
123456789
<combo-box name="item" filter="contains"> ...</combo-box> <combo-box name="item" filter="startsWith"> ...</combo-box>
Multi-Select Mode
Add multiple to switch to multi-select tag mode. Users select multiple items which appear as removable tag chips. Selected options are hidden from the dropdown to prevent duplicates.
123456789
<label for="topics">Topics</label><combo-box name="topics" multiple> <input type="text" id="topics" placeholder="Search topics..."> <ul> <li data-value="js">JavaScript</li> <li data-value="css">CSS</li> <li data-value="html">HTML</li> </ul></combo-box>
Multi-Select Options
In multi mode, use max to limit the number of selections and custom to let users type custom entries (press Enter or comma to add). Each selected tag is submitted as a separate FormData entry (like multiple checkboxes with the same name).
12345678910111213141516171819202122
<form id="my-form"> <label for="skills">Skills</label> <combo-box name="skills" multiple required max="5" custom> <input type="text" id="skills" placeholder="Add skills..."> <ul> <li data-value="design">Design</li> <li data-value="frontend">Frontend</li> <li data-value="backend">Backend</li> </ul> </combo-box> <button type="submit">Submit</button></form> <script> document.getElementById('my-form') .addEventListener('submit', (e) => { e.preventDefault(); const data = new FormData(e.target); console.log('skills:', data.getAll('skills')); });</script>
Attributes
| Attribute | Values | Default | Description |
|---|
name | string | — | Form field name. A name on the inner input (the no-JS path) moves here on upgrade so exactly one value submits. |
required | boolean | — | Require a selection for form validation |
disabled | boolean | — | Disables the input and tag remove buttons, closes the listbox and drops the value from submission. A disabled ancestor fieldset has the same effect. |
filter | "contains", "startsWith" | contains | How typing filters the options |
value | string | — | Selected value (single mode) |
placeholder | string | — | Input placeholder text |
multiple | boolean | — | Enable multi-select tag mode |
max | number | — | Maximum number of tags (multi mode) |
custom | boolean | — | Allow typed entries via Enter/comma (multi mode) |
Required Structure
| Element | Required | Description |
|---|
<input type="text"> | yes | Text input for filtering and display. Give it an id and point a <label for> at it — the id and labels are preserved and name the combobox. |
<ul> or <ol> | yes | Options list container |
<li data-value> | yes | Individual option — one per selectable choice |
Child Attributes
| Attribute | On | Values | Description |
|---|
data-value | li | string | Option value identifier placed on each <li> in the options list |
name | input | string | Submission name for the no-JS fallback (submits the typed text). Moved to the host on upgrade (the host's own name wins if both are set). |
Events
| Event | Detail | Description |
combo-box:change | { value, label, source } | Fired when the selected option changes (single mode). Re-choosing the current option closes the list without firing. |
combo-box:change | { values: string[], labels: string[], source } | Fired when tags are added or removed (multi mode). |
combo-box:open | — | Fired when the listbox opens. |
combo-box:close | — | Fired when the listbox closes. |
Single Mode
123456789101112131415
const combo = document.querySelector('combo-box'); combo.addEventListener('combo-box:change', (e) => { if (e.detail.source === 'api') return; console.log('Selected:', e.detail.value, e.detail.label);}); combo.addEventListener('combo-box:open', () => { console.log('Listbox opened');}); combo.addEventListener('combo-box:close', () => { console.log('Listbox closed');});
Multi Mode
123456
const combo = document.querySelector('combo-box[multiple]'); combo.addEventListener('combo-box:change', (e) => { console.log('Values:', e.detail.values); console.log('Labels:', e.detail.labels);});
JavaScript API
| Property | Type | Mode | Description |
element.value | string | Single | Current selected value (read/write) |
element.label | string | Single | Current selected label (read-only) |
element.values | string[] | Multi | Array of selected tag values (read-only) |
element.labels | string[] | Multi | Array of selected tag labels (read-only) |
Single Mode
1234567891011
const el = document.querySelector('combo-box'); console.log(el.value); console.log(el.label); el.value = 'gb'; el.value = '';
Multi Mode
12345
const el = document.querySelector('combo-box[multiple]'); console.log(el.values); console.log(el.labels);
Keyboard Navigation
| Key | Action |
| ArrowDown | Open listbox / move to next option |
| ArrowUp | Open listbox / move to previous option |
| Home | Jump to first option |
| End | Jump to last option |
| Enter | Select option (single) or add tag (multi). With custom: add typed text. With no active option and nothing to add, Enter submits the surrounding form. |
| Escape | Close listbox |
| Tab | Close listbox and move focus |
| Backspace | Remove last tag when input is empty (multi mode) |
| , (comma) | Add custom tag (multi mode with custom) |
Accessibility
ARIA Pattern
Implements the W3C ARIA Combobox pattern:
- Input has
role="combobox", aria-expanded, aria-autocomplete="list", and aria-controls
- List has
role="listbox" with an auto-generated ID
- Options have
role="option" and aria-selected
- Active option announced via
aria-activedescendant
- Multi mode adds
aria-multiselectable="true" and an aria-live region for tag add/remove announcements
Form Validation
Validity is set on the host with ElementInternals.setValidity(), anchored to the inner input, so the browser’s validation bubble points at the input and the host matches :invalid, :user-valid and :user-invalid. The host is not focusable, so validity on it alone is not announced. When the browser reports the field invalid (on submit or checkValidity()), the component sets aria-invalid="true" on the focusable input, and removes it once a valid selection is made or the form is reset. It is deliberately not set on page load, before the user has interacted.
Form Association
This component is a Form-Associated Custom Element. It uses ElementInternals to:
- Submit the selected
value via FormData (single mode: one value; multi mode: one entry per tag)
- Validate with
setValidity() for required fields
- Reset on form reset via
formResetCallback()
- Restore state from browser history via
formStateRestoreCallback() (single mode)
No hidden inputs needed. The name can sit on the host or on the inner input (for the no-JS path); either way exactly one value is submitted after upgrade. A disabled host (attribute or <fieldset disabled>) submits nothing.
Styling
The component uses @scope (combo-box) for shared CSS encapsulation and @scope (combo-box[multiple]) for tag-specific styles. Override option states and tag chip appearance using standard selectors:
1234567891011121314151617181920212223242526
combo-box li[data-value]:hover { background: var(--color-surface-alt);} combo-box li[data-active] { outline: 2px solid var(--color-interactive);} combo-box li[aria-selected="true"] { font-weight: 500; color: var(--color-interactive);} combo-box[multiple] .tag { background: var(--color-surface-raised); border: var(--border-width-thin) solid var(--color-border); border-radius: var(--radius-pill);} combo-box[multiple] .tag button:hover { color: var(--color-error);}
Progressive Enhancement
Without JavaScript, <combo-box> renders as a plain text input above a visible, scrollable list. Users can still see all options and type into the input, and a name on the input submits the typed text. Once JS loads, the component adds filtering, keyboard navigation, and form association, and submits the option’s data-value instead.
1234567891011121314
combo-box:not(:defined) { display: block;} combo-box:not(:defined) > ul { list-style: none; padding: 0; margin-block-start: var(--size-xs); border: var(--border-width-thin) solid var(--color-border); border-radius: var(--radius-m); max-block-size: 12rem; overflow-y: auto;}