A Typeahead is something like Google’s search. When you type something into the search bar, you get a dropdown of items you can select from.
We’re going to build a Typeahead that helps users type a country. Here’s what we’re building together.
Typeahead vs Autocomplete
Typeahead can also be called Autocomplete because it helps users to complete what they’re typing.
Browsers have a feature called autocomplete. The Typeahead we’re building is different from browsers’ autocomplete feature. Do not get confused!
Browsers’ autocomplete feature (sometimes called autofill) allows users filled things they filled in before—even if they’re things from other websites. They’re commonly used to fill stuff like name, address, and credit card information.
In contrast, Typeahead predicts what users want to type. It can contain any kind of information.
Anatomy of a Typeahead
A Typeahead contains two elements:
An input field
A list
The input field allows users to type things. We’ll add things to the list based on things they typed.
Building the Typeahead (HTML and CSS)
Typeahead are usually used in forms, so we’ll place our typeahead inside a <form>. Make sure you set the form’s autocomplete to off to disable browsers’ autocomplete feature.