twitter typeahead.js

All posts tagged twitter typeahead.js


Twitter Typeahead.js has been updated and there are a lot of changes.  I’m going to give a an explanation for a few of their examples and explain the key features and changes that they have made.  For those looking to upgrade to this new version it will require a rewrite of your current Typeahead.js methods so please be aware of that.  The biggest changes are outlined in their changelog.

The most important change in 0.10.0 is that typeahead.js was broken up into 2 individual components: Bloodhound and jQuery#typeahead. Bloodhound is an feature-rich suggestion engine. jQuery#typeahead is a jQuery plugin that turns input controls into typeaheads.

Let’s head to the examples.

The most basic of basic.  Here we have a static list of local data.  Bloodhound needs to be initialized with your data and you can see it handles splitting data up by white space with their tokenizers.  This makes things a lot easier because in previous versions you needed to make sure the tokens to be searched through were already split up by white space.  The Bloodhound object is then initialized and the typeahead object is created.  This is a lot more code but it makes the search more robust as we will see with a more advanced example using prefetch and remote data.

Here we see a very similar example to the one above except that we are getting our data from a URL.  The prefetch variable works to get a set of data and cache it locally.  Even if the page is reloaded that data stays with the user’s browser until it expires or is removed.  As the user begins their search it will have that data ready and on hand but if there is no data to be found in the prefetch Typeahead.js has another URL, stored in the remote variable, that it can make a query to and get data back about what the user is searching for.  These two in conjunction work fantastically well for supplying the user with a set of data that has the speed of being static but can be dynamic by querying the URL for more data as the user goes.  Again we initialize the Bloodhound object and add it to the typeahead initialization.  We are again using the Handlebars template rendering engine to make the search results look great so that hasn’t changed.

The changes are drastic but not hard.  This is a major update to Twitter Typeahead.js and because of that there is deprecation.  My other posts are still useful for older versions but may not apply to versions going forward.  I hope this was helpful in getting you up to speed with the latest version of Twitter Typeahead.js.  If you have any questions or comments feel free to leave them below.


Since Bootstrap dropped support for their typeahead they have since told people to use Twitter’s typeahead.js.  It has a lot of great features that I have covered in an older write up about Twitter Typeahead.js.  In this post I wanted to go over the custom events that are triggered and how you can use them to get more out of Twitter Typehead.

There are five triggers in the typeahead.js and you can tap into each of these to do what you’d like when these events are triggered.  Below are those events and when they are triggered.

  • typeahead:initialized – Triggered after initialization. If data needs to be prefetched, this event will not be triggered until after the prefetched data is processed.
  • typeahead:opened – Triggered when the dropdown menu of a typeahead is opened.
  • typeahead:closed – Triggered when the dropdown menu of a typeahead is closed.
  • typeahead:selectedTriggered when a suggestion from the dropdown menu is explicitly selected. The datum for the selected suggestion is passed to the event handler as an argument in addition to the name of the dataset it originated from.  Triggered when the user hits Tab to complete the text provided in the text box.  See comments section below.
  • typeahead:autocompletedTriggered when the query is autocompleted. The datum used for autocompletion is passed to the event handler as an argument in addition to the name of the dataset it originated from.  Triggered when the user clicks, or uses the arrow keys, to select an element from the drop down.  See comments section below.

All of that information comes directly from the typeahead README.  Now that we know what the events are and when they are triggered we can make custom events that happen by using jQuery and tapping into those events using .on().  Say, for example, I wanted to update a title on a page including the input field that typeahead already does.  You could make an on change event that would listen on that input for a change and then alter the title but in our case we can simplify that by tapping into the typeahead:selected event.

Now instead of a second listener on your input object you can just have Twitter Typeahead listen for you and make changes where you need.  Play around with the different variations of the events to help you get the most out of Twitter Typeahead.  There is an issue on their GitHub page that explains how to use the custom events and gives a very basic example.  Below is the example and here is the link to Getting a custom event to fire.

Hope that helps someone as it took me a bit of digging to find the easiest way to use those triggers.  Comment below on other useful ways you’ve used Twitter Typeahead and the custom event triggers.