question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Example of typeahead:selected event

See original GitHub issue

For anyone interested until an example is added to documentation for custom events…

Assuming there’s an input on your page called ‘search_input’:


$('#search_input').bind('typeahead:selected', function(obj, datum, name) {      
        alert(JSON.stringify(obj)); // object
        // outputs, e.g., {"type":"typeahead:selected","timeStamp":1371822938628,"jQuery19105037956037711017":true,"isTrigger":true,"namespace":"","namespace_re":null,"target":{"jQuery19105037956037711017":46},"delegateTarget":{"jQuery19105037956037711017":46},"currentTarget":
        alert(JSON.stringify(datum)); // contains datum value, tokens and custom fields
        // outputs, e.g., {"redirect_url":"http://localhost/test/topic/test_topic","image_url":"http://localhost/test/upload/images/t_FWnYhhqd.jpg","description":"A test description","value":"A test value","tokens":["A","test","value"]}
        // in this case I created custom fields called 'redirect_url', 'image_url', 'description'   

        alert(JSON.stringify(name)); // contains dataset name
        // outputs, e.g., "my_dataset"

});

https://github.com/twitter/typeahead.js#custom-events

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:17
  • Comments:41 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
ZaneCEOcommented, Jul 10, 2015

I was banging my head against the wall so… please note that now the event is “typeahead:select” and NO LONGER “selected”… after this, the original example still works and is the correct way to do it, thanks!

1reaction
aaron-nunleycommented, Apr 18, 2014

@maanasa Give this a shot? Looks like you were binding to the input field itself rather than the typeahead object.

$('#field_id').typeahead({
  highlight: true
},
{
  name: 'first_dataset',
  displayKey: 'name',
  source: first_dataset.ttAdapter(),
  templates: {
    header: '<b>first_dataset</b><hr />'
  }
},
{
  name: 'second_dataset',
  displayKey: 'name',
  source: second_dataset.ttAdapter(),
  templates: {
    header: '<b>second_dataset</b><hr />'
  }
}).bind("typeahead:selected", function(obj, datum, name) {
console.log(obj, datum, name);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Handle selected event in autocomplete textbox using ...
I want to run JavaScript function just after user select a value using autocomplete textbox bootstrap Typeahead. I'm ...
Read more >
Using Twitter Typeahead.js Custom Event Triggers | Blog
typeahead:selected – Triggered when a suggestion from the dropdown menu is explicitly selected. The datum for the selected suggestion is passed to the...
Read more >
Twitter Typeahead custom event demo - CodePen
Simple demo for a [Stack Overflow Answer](http://stackoverflow.com/questions/18710325/twitter-bootstrap-typeahead-custom-keypress-enter-function/187108...
Read more >
typeahead.js – examples - Twitter Open Source
When initializing a typeahead using the typeahead.js jQuery plugin, you pass the plugin method one or more datasets. The source of a dataset...
Read more >
jQuery | Autocomplete Selection Event - GeeksforGeeks
The very basic purpose of this feature is to replace the value of the text field with the selected value from the list...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found