Editable Combobox With Both List and Inline Autocomplete Example
The below combobox for choosing the name of a US state or territory demonstrates the design pattern for combobox. The design pattern describes four types of autocomplete behavior. This example illustrates the autocomplete behavior referred to in the pattern as list with inline completion. 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, and the first match is automatically selected. In addition, the portion of the selected suggestion that has not been typed by the user, a completion string, appears inline after the input cursor in the textbox. The automatically selected suggestion becomes the value of the textbox when the combobox loses focus unless the user chooses a different suggestion or changes the character string in the textbox. 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:
- Select-Only Combobox: A single-select combobox with no text input that is functionally similar to an HTML
select
element. - Editable Combobox with List Autocomplete: An editable combobox that demonstrates the autocomplete behavior known as
list with manual selection
. - Editable Combobox Without Autocomplete: An editable combobox that demonstrates the behavior associated with
aria-autocomplete=none
. - Editable Combobox with Grid Popup: An editable combobox that presents suggestions in a grid, enabling users to navigate descriptive information about each suggestion.
- Date Picker Combobox: An editable date input combobox that opens a dialog containing a calendar grid and buttons for navigating by month and year.
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
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 design pattern.
Textbox
Key | Function |
---|---|
Down Arrow |
|
Alt + Down Arrow | Opens the listbox without moving focus or changing selection. |
Up Arrow |
|
Enter |
|
Escape |
|
Standard single line text 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
Using aria-activedescendant to Manage Focus.
Key | Function |
---|---|
Enter |
|
Escape |
|
Down Arrow |
|
Up Arrow |
|
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 |
|
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 design pattern.
Textbox
Role | Attribute | Element | Usage |
---|---|---|---|
combobox
|
input[type="text"] |
Identifies the input as a combobox. | |
aria-autocomplete="both"
|
input[type="text"] |
Indicates that the autocomplete behavior of the text input is to both show an inline completion string and suggest a list of possible values in a popup where the suggestions are related to the string that is present in the textbox. | |
aria-controls="#IDREF"
|
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"] |
|
|
aria-activedescendant="IDREF"
|
input[type="text"] |
|
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 |
|
|
aria-selected="true"
|
li |
|
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="#IDREF"
|
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
- CSS: combobox-autocomplete.css
- Javascript: combobox-autocomplete.js
HTML Source Code