Implement an autocomplete feature that enhances user experience by suggesting possible completions for a search term as it is being typed into an HTML input field. This functionality should dynamically filter and display a list of suggestions based on the current input value, offering keyboard navigation to select from the filtered results. The ARIA combobox pattern is a good accessibility reference.
Task Overview
Your task is to write a JavaScript function that integrates with an HTML input field, monitoring the user’s input. As the user types, the function should interact with a predefined endpoint (simulated by an object in this case) that returns a list of potential matches. These matches should then be displayed as a list of suggestions beneath the input field, updating in real time with each keystroke to reflect the current query.
Autocomplete Search List: Features and Behavior
- The autocomplete should dynamically filter search results to include only items matching the current input, disregarding case sensitivity.
- The displayed results should update as the user types, with each keystroke refining the list of suggestions.
- Keyboard navigation (e.g., using the arrow keys) should allow the user to browse through the list of suggestions and select a result to fill in the search field.
- Each suggestion in the list should be selectable, and clicking on a suggestion or pressing ‘Enter’ when highlighted should populate the input field with that value.
Example
Consider an input field where a user starts typing “Al”. The autocomplete feature should filter the list of names to show only those that contain “Al”, such as “Alex” and “Aleksandra”, beneath the input field as selectable suggestions.
