Jeremy
Jeremy

Reputation: 5435

Twitter Typeahead data passing

I'm using twitter typeahead bundle. I have code like this:

   var results = new bloodhound({...}) 
   //results is a map in json with the keys ID and displayName. It's  grabbed by ajax call.

    $('#element').typeahead({
    ...
    {
      name: 'Results',
      displayKey: 'displayName',
      source: results.ttAdapter(),
    }...

This works very, very well. The results are being populated as users type stuff. However, one issue I have is that when the user clicks "submit", the value I need to grab is not the displayName, but instead the ID.

If the user types, "Exam", let's say the results map looks like: {"id":"100","displayName":"Example1"}. They click on "Example1" and example1 jumps to the text box. However, when the user clicks submit, I want "100" to come from that element, rather than "Example1". Is that possible?

Upvotes: 0

Views: 222

Answers (1)

O'Mutt
O'Mutt

Reputation: 1592

You could use the typeahead selected handler

.on("typeahead:selected", function(event, item) {
    $("#hiddenInputToSubmit").val(item.id);
    return;
}

Maybe that could work for you?

Upvotes: 1

Related Questions