Editable Combobox without Autocomplete Example

About This Example

The below combobox that enables users to choose a term from a hypothetical list of previously searched terms demonstrates the Combobox Pattern. The design pattern describes four types of autocomplete behavior. This example illustrates the autocomplete behavior known as no autocomplete. The terms that appear in the listbox popup are not related to the string that is present in the textbox. In this implementation, the listbox popup is not automatically triggered when the textbox receives focus; the list is opened when the user types a character into the textbox or through an explicit open command.

Similar examples include:

Example

  • weather
  • salsa recipes
  • cheap flights to NY
  • dictionary
  • baseball scores
  • hotels in NY
  • mortgage calculator
  • restaurants near me
  • free games
  • gas prices
  • classical music

Accessibility Features

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
  • First opens the listbox if it is not already displayed and then moves visual focus to the first option.
  • DOM focus remains on the textbox.
Alt + Down Arrow Opens the listbox without moving focus or changing selection.
Up Arrow
  • First opens the listbox if it is not already displayed and then moves visual focus to the last option.
  • DOM focus remains on the textbox.
Enter Closes the listbox if it is displayed.
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 not filtered based on the 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="none" input[type="text"] Indicates that the suggestions in the combobox popup are not values that complete the current textbox input.
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="Previous Searches" 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, since it's keyboard function is redundant with the keyboard operation of the textbox to open the listbox.
aria-label="Previous Searches" 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