Typeahead Component Documentation

Description

Typeahead.js a flexible JavaScript library that provides a strong foundation for building robust typeaheads.

Initialization

Initialize the plugin by referencing the necessary files:

<script src="typeahead.bundle.min.js"></script>
    <script src="bloodhound.min.js"></script>
    <script src="handlebars.js"></script>

Code Structure

Prefetched data is fetched and processed on initialization. If the browser supports local storage, the processed data will be cached there to prevent additional network requests on subsequent page loads.

var countries = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.whitespace,
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  // url points to a json file that contains an array of country names, see
  // https://github.com/twitter/typeahead.js/blob/gh-pages/data/countries.json
  prefetch: '../data/countries.json'
});

// passing in `null` for the `options` arguments will result in the default
// options being used
$('#prefetch .typeahead').typeahead(null, {
  name: 'countries',
  source: countries
});