SyntaxStudy
Sign Up
jQuery Typeahead / Autocomplete Plugin
jQuery Advanced 4 min read

Typeahead / Autocomplete Plugin

Typeahead.js

Typeahead.js provides a powerful autocomplete with bloodhound suggestion engine. It supports local datasets and remote endpoints.

Example
<script src="https://cdn.jsdelivr.net/npm/typeahead.js/dist/typeahead.bundle.min.js"></script>

<script>
const countries = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace("name"),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: { url: "/api/countries?q=%QUERY", wildcard: "%QUERY" }
});

$("#country-input").typeahead(
  { hint: true, highlight: true, minLength: 1 },
  { name: "countries", display: "name", source: countries }
);
</script>
Pro Tip

Bloodhound caches remote results locally to reduce AJAX requests on repeated searches.