Use a regex to wrap the matching part in a <strong> tag: text.replace(new RegExp(`(${query})`, 'gi'), '<strong>$1</strong>'). Style strong with a different color to highlight the match.
How do you highlight matching text in an autocomplete search bar?
More FAQs in Building an Autocomplete Search Bar: A Complete Guide
Create an input and a suggestions list. Debounce the input (300ms). Fetch suggestions on input. Render filtered results. Add keyboard navigation (ArrowUp/Down, Enter, Escape). Highlight matching text. Close on click outside. Show loading and no-results states.
To avoid making an API call on every keystroke. Debounce delays the fetch until the user stops typing for 300ms. This reduces API calls, improves performance, and provides a better user experience.
Track a currentHighlight index. ArrowDown increments, ArrowUp decrements. Enter triggers the highlighted item's click. Escape closes the suggestions. Toggle a 'highlighted' CSS class on the active item.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.