Skip to content

Editable Combobox With List Autocomplete Example

Editable Combobox With List Autocomplete Example

Read This First

The code in this example is not intended for production environments. Before using it for any purpose, read this to understand why.

This is an illustrative example of one way of using ARIA that conforms with the ARIA specification.

About This Example

The below combobox for choosing the name of a US state or territory demonstrates the Combobox Pattern. The design pattern describes four types of autocomplete behavior. This example illustrates the autocomplete behavior known as list autocomplete with manual selection. If the user types one or more characters in the edit box and the typed characters match the beginning of the name of one or more states or territories, a listbox popup appears containing the matching names. When the listbox appears, a suggested name is not automatically selected. Thus, after typing, if the user tabs or clicks out of the combobox without choosing a value from the listbox, the typed string becomes the value of the combobox. Note that this implementation enables users to input the name of a state or territory, but it does not prevent input of any other arbitrary value.

Similar examples include:

Example

  • Alabama
  • Alaska
  • American Samoa
  • Arizona
  • Arkansas
  • California
  • Colorado
  • Connecticut
  • Delaware
  • District of Columbia
  • Florida
  • Georgia
  • Guam
  • Hawaii
  • Idaho
  • Illinois
  • Indiana
  • Iowa
  • Kansas
  • Kentucky
  • Louisiana
  • Maine
  • Maryland
  • Massachusetts
  • Michigan
  • Minnesota
  • Mississippi
  • Missouri
  • Montana
  • Nebraska
  • Nevada
  • New Hampshire
  • New Jersey
  • New Mexico
  • New York
  • North Carolina
  • North Dakota
  • Northern Marianas Islands
  • Ohio
  • Oklahoma
  • Oregon
  • Pennsylvania
  • Puerto Rico
  • Rhode Island
  • South Carolina
  • South Dakota
  • Tennessee
  • Texas
  • Utah
  • Vermont
  • Virginia
  • Virgin Islands
  • Washington
  • West Virginia
  • Wisconsin
  • Wyoming

Accessibility Features

  • Browsers do not manage visibility of elements referenced by aria-activedescendant like they do for elements with focus. When a keyboard event changes the active option in the listbox, the JavaScript scrolls the option referenced by aria-activedescendant into view. Managing aria-activedescendant visibility is essential to accessibility for people who use a browser's zoom feature to increase the size of content.
  • To enhance perceivability when operating the combobox, visual keyboard focus and hover are styled using the CSS :hover pseudo-class, and focus and blur event handlers:
    • To make it easier to perceive when the combobox receives focus, focus creates a 2 pixel focus ring around both the input and button elements with 2 pixels of space between the focus ring and the combobox.
    • To make it easier to perceive that either the input or button can be clicked to open the listbox, hover causes the same styling as focus.
    • To help people with visual impairments identify the combobox as an interactive element, the cursor is changed to a pointer when hovering over the combobox or list.
    • To make it easier to distinguish the selected listbox option from other options, selection creates a 2 pixel border above and below the option.
    • Note: Because transparent borders are visible on some systems with operating system high contrast settings enabled, transparency cannot be used to create a visual difference between the element that is focused an other elements. Instead of using transparency, the focused element has a thicker border and less padding. When an element receives focus, its border changes from zero to two pixels and padding is reduced by two pixels. When an element loses focus, its border changes from two pixels to two and padding is increased by two pixels.
  • To ensure the inline SVG graphic used for the arrow in the open button has sufficient contrast with the background when high contrast settings change the color of text content, CSS forced-color-adjust is set to auto on the svg element and and the fill attribute of the polygon element is set to currentcolor. This causes the colors of the fill of the polygon elements to be overridden by the high contrast color setting. If forced-color-adjust were not used to override the colors specified for the fill property, the color would remain the same in high contrast mode, which could lead to insufficient contrast between the arrow and the background or even make it invisible if the color matched the high contrast mode background.
    Note: The explicit setting of forced-color-adjust is necessary because some browsers do not use auto as the default value for SVG graphics.

Keyboard Support

The example combobox on this page implements the following keyboard interface. Other variations and options for the keyboard interface are described in the Keyboard Interaction section of the Combobox Pattern.

Textbox

Key Function
Down Arrow
  • If the textbox is not empty and the listbox is displayed, moves visual focus to the first suggested value.
  • If the textbox is empty and the listbox is not displayed, opens the listbox and moves visual focus to the first option.
  • In both cases DOM focus remains on the textbox.
Alt + Down Arrow Opens the listbox without moving focus or changing selection.
Up Arrow
  • If the textbox is not empty and the listbox is displayed, moves visual focus to the last suggested value.
  • If the textbox is empty, first opens the listbox if it is not already displayed and then moves visual focus to the last option.
  • In both cases DOM focus remains on the textbox.
Enter Closes the listbox if it is displayed.
Escape
  • If the listbox is displayed, closes it.
  • If the listbox is not displayed, clears the textbox.
Standard single line text editing keys
  • Keys used for cursor movement and text manipulation, such as Delete and Shift + Right Arrow.
  • An HTML input with type="text" is used for the textbox so the browser will provide platform-specific editing keys.

Listbox Popup

NOTE: When visual focus is in the listbox, DOM focus remains on the textbox and the value of aria-activedescendant on the textbox is set to a value that refers to the listbox option that is visually indicated as focused. Where the following descriptions of keyboard commands mention focus, they are referring to the visual focus indicator. For more information about this focus management technique, see Managing Focus in Composites Using aria-activedescendant.

Key Function
Enter
  • Sets the textbox value to the content of the focused option in the listbox.
  • Closes the listbox.
  • Sets visual focus on the textbox.
Escape
  • Closes the listbox.
  • Sets visual focus on the textbox.
Down Arrow
  • Moves visual focus to the next option.
  • If visual focus is on the last option, moves visual focus to the first option.
  • Note: This wrapping behavior is useful when Home and End move the editing cursor as described below.
Up Arrow
  • Moves visual focus to the previous option.
  • If visual focus is on the first option, moves visual focus to the last option.
  • Note: This wrapping behavior is useful when Home and End move the editing cursor as described below.
Right Arrow Moves visual focus to the textbox and moves the editing cursor one character to the right.
Left Arrow Moves visual focus to the textbox and moves the editing cursor one character to the left.
Home Moves visual focus to the textbox and places the editing cursor at the beginning of the field.
End Moves visual focus to the textbox and places the editing cursor at the end of the field.
Printable Characters
  • Moves visual focus to the textbox.
  • Types the character in the textbox.
  • Options in the listbox are filtered based on characters in the textbox.

Button

The button has been removed from the tab sequence of the page, but is still important to assistive technologies for mobile devices that use touch events to open the list of options.

Role, Property, State, and Tabindex Attributes

The example combobox on this page implements the following ARIA roles, states, and properties. Information about other ways of applying ARIA roles, states, and properties is available in the Roles, States, and Properties section of the Combobox Pattern.

Textbox

Role Attribute Element Usage
combobox input[type="text"] Identifies the input as a combobox.
aria-autocomplete="list" input[type="text"] Indicates that the autocomplete behavior of the text input is to suggest a list of possible values in a popup and that the suggestions are related to the string that is present in the textbox.
aria-controls="ID_REF" input[type="text"] Identifies the element that serves as the popup.
aria-expanded="false" input[type="text"] Indicates that the popup element is not displayed.
aria-expanded="true" input[type="text"] Indicates that the popup element is displayed.
id="string" input[type="text"]
  • Referenced by for attribute of label element to provide an accessible name.
  • Recommended naming method for HTML input elements because clicking label focuses input.
aria-activedescendant="ID_REF" input[type="text"]
  • When an option in the listbox is visually indicated as having keyboard focus, refers to that option.
  • When navigation keys, such as Down Arrow, are pressed, the JavaScript changes the value.
  • Enables assistive technologies to know which element the application regards as focused while DOM focus remains on the input element.
  • For more information about this focus management technique, see Managing Focus in Composites Using aria-activedescendant.

Listbox Popup

Role Attribute Element Usage
listbox ul Identifies the ul element as a listbox.
aria-label="States" ul Provides a label for the listbox.
option li
  • Identifies the element as a listbox option.
  • The text content of the element provides the accessible name of the option.
aria-selected="true" li
  • Specified on an option in the listbox when it is visually highlighted as selected.
  • Occurs only when an option in the list is referenced by aria-activedescendant.

Button

Role Attribute Element Usage
tabindex="-1" button Removes the button from the tab sequence of the page because its function is redundant with the keyboard operation of the combobox.
aria-label="States" button Provides a label for the button.
aria-controls="ID_REF" button Identifies the element that serves as the popup.
aria-expanded="false" button Indicates that the popup element is not displayed.
aria-expanded="true" button Indicates that the popup element is displayed.

Javascript and CSS Source Code

HTML Source Code

Back to Top

This is an unpublished draft preview that might include content that is not yet approved. The published website is at w3.org/WAI/.