16

I'm trying to use Twitter typeahead.js to return a datum after selection. From what I understood by the documentation the code should look something like

$('.selector').typeahead({
  name: 'identifier',
  local: localObjectsArray
}).on('autocompleted', function(item){
    alert(JSON.stringify(item));
});

However this doesn't work. What's the proper way to detect typeahead events?

Hieu Nguyen
  • 8,563
  • 2
  • 36
  • 43
Sovos
  • 3,330
  • 7
  • 25
  • 36

1 Answers1

45

After selection, so you need typeahead:selected:

$('.selector').typeahead({
    name: 'identifier',
    local: localObjectsArray
}).on('typeahead:selected', function (obj, datum) {
    console.log(obj);
    console.log(datum);
});

Hope it helps.

Hieu Nguyen
  • 8,563
  • 2
  • 36
  • 43
  • 3
    This is a good tip, but should probably be edited to use .on() instead of .bind() See http://stackoverflow.com/questions/8065305/whats-the-difference-between-on-and-live-or-bind – dualmon Nov 02 '13 at 15:59
  • I think using `.on()` is overkill for this, I will update the answer when `.bind()` is deprecated. – Hieu Nguyen Nov 05 '13 at 10:22
  • 4
    Fair enough, but just for the record, "As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers" from http://api.jquery.com/bind/ – dualmon Nov 08 '13 at 06:35
  • 1
    mine doesn't ever have "selected" attached so I can't use this method...been looking everywhere and can't find a solution to my issue. There seems to be so little use of typeahead.js to begin with. If you can help me here please let me know, @Hieu http://stackoverflow.com/questions/24813018/twitter-typeahead-js-with-scrollto-not-working – user2847749 Jul 22 '14 at 17:59
  • 4
    Please note that the custom typeahead event is now `typeahead:select` instead of `typeahead:selected`. I'm unsure when this change was made, but as of v0.11.1 `typeahead.selected` isn't defined. The docs reflect this: https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#custom-events – StickByAtlas Dec 16 '15 at 19:06