stkvtflw
stkvtflw

Reputation: 13507

How to handle objects in typeahead?

For sake of simplicity, let's base this on first simplest example from here: https://twitter.github.io/typeahead.js/examples/

Here is the suggestions array for this example:

var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
  'Colorado', 'Connecticut'  etc..
];

I want to have objects in this array:

var states = [
    {name: 'Alabama', image: 'images/Alabama.jpg', objectId: '123'},
    {name: 'Alaska', image: 'images/Alaska.jpg', objectId: '341'},
    {name: 'Arizona', image: 'images/Arizona.jpg' objectId: '546'},
];

and when I will push 'enter' key or click on one of those suggestions, I need to extract this particular object and .push it to another array. How do I implement this?

Upvotes: 0

Views: 105

Answers (1)

Prem Bikram Limbu
Prem Bikram Limbu

Reputation: 159

$(textbox-id).on('typeahead:selected', function (e, datum) {
    console.log(datum); //object
    console.log(datum.value); // will print Value you have clicked like Alabama is selected then Alabama is printed in the console.

Upvotes: 1

Related Questions