Bootstrap Typeahead
Learn how to build search inputs that suggest matching results while the user types.
Introduction
Typeahead is a search feature that displays suggestions based on the text entered by a user. It helps users find options quickly without typing the complete value. Bootstrap 5 does not include a built-in Typeahead plugin, so this example uses Bootstrap styling and JavaScript.
Learning Objectives
- Understand the Typeahead concept.
- Create a search input with suggestions.
- Filter suggestions while typing.
- Select a suggestion from the list.
- Display a message when no result matches.
How Typeahead Works
Step 1
The user types a keyword.
Step 2
JavaScript finds matching items.
Step 3
The selected value fills the input.
1. Basic Typeahead Search
In this example, JavaScript filters a predefined list of programming courses whenever the user types at least one character. Matching courses appear below the input.
Program Code
<input type="text"
id="courseSearch"
class="form-control"
placeholder="Search courses..."
autocomplete="off">
<ul id="courseSuggestions"
class="list-group mt-2"></ul>
<script>
const courses = [
"Java Full Stack",
"Python Full Stack",
".NET Full Stack",
"Data Analytics"
];
const search = document.getElementById("courseSearch");
const suggestions =
document.getElementById("courseSuggestions");
search.addEventListener("input", function () {
const query = this.value.trim().toLowerCase();
suggestions.innerHTML = "";
if (query === "") return;
const matches = courses.filter(course =>
course.toLowerCase().includes(query)
);
matches.forEach(course => {
const item = document.createElement("li");
item.className = "list-group-item list-group-item-action";
item.textContent = course;
item.addEventListener("click", function () {
search.value = course;
suggestions.innerHTML = "";
});
suggestions.appendChild(item);
});
});
</script>
Program Output
2. Typeahead with No Results Message
If no item matches the search text, display a message such as "No matching courses found." This makes the search experience clearer for the user.
Program Code
const matches = courses.filter(course =>
course.toLowerCase().includes(query)
);
if (matches.length === 0) {
const item = document.createElement("li");
item.className = "list-group-item text-muted";
item.textContent = "No matching courses found.";
suggestions.appendChild(item);
}
Program Output
3. Keyboard-Friendly Search
Users can navigate suggestions with the keyboard. This example supports the Up Arrow, Down Arrow, Enter and Escape keys.
Program Code
search.addEventListener("keydown", function (event) {
if (event.key === "ArrowDown") {
event.preventDefault();
activeIndex = Math.min(
activeIndex + 1,
matches.length - 1
);
}
if (event.key === "ArrowUp") {
event.preventDefault();
activeIndex = Math.max(activeIndex - 1, 0);
}
if (event.key === "Escape") {
suggestions.innerHTML = "";
}
if (event.key === "Enter" && activeIndex >= 0) {
event.preventDefault();
search.value = matches[activeIndex];
suggestions.innerHTML = "";
}
});
Program Output
Use Down Arrow and Up Arrow to navigate, Enter to select, or Escape to close suggestions.
Important Classes and Methods
| Class / Method | Purpose |
|---|---|
form-control |
Styles the search input. |
list-group |
Styles the suggestions container. |
list-group-item |
Styles an individual suggestion. |
addEventListener("input", ...) |
Runs filtering when the input changes. |
filter() |
Returns items that match a condition. |
includes() |
Checks whether text contains a substring. |
textContent |
Sets suggestion text safely as plain text. |
Real-Life Example
On a training institute website, a student starts typing "Java" in the course search box. The interface suggests Java Full Stack and related courses. The student can select an option instead of typing the complete name.
Common Mistakes
- Forgetting to clear old suggestions before displaying new ones.
- Ignoring case differences during matching.
- Not handling empty input or unmatched results.
- Using duplicate IDs for search inputs and suggestion lists.
- Assuming Bootstrap 5 includes a built-in Typeahead plugin.
Summary
- Typeahead displays suggestions while the user types.
- JavaScript can filter a list of available options.
- Bootstrap classes provide the visual styling.
- Users can select suggestions to fill the input.
- Keyboard navigation improves accessibility.