Pantamtuy
Pantamtuy

Reputation: 243

Twitter typeahead different value to display

    //javascript
    var customer = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,

    prefetch: 'include/customer.json'
    });

    $('.typeahead').typeahead(null, {
    name: 'customer',
    display: 'name',
    source: customer
    });


    //json
    [
    {"identity":"1","name":"Uzumaki Naruto"},
    {"identity":"2","name":"Monkey D. Luffy"},
    {"identity":"3","name":"Ichigo Kurosaki"}
    ]

   //html
   <input class="typeahead" type="text" name="customer"/>

is it possible in typeahead plugin to search and display the 'name' and when i submit the form it will give me the value of 'identity'. Btw i`m using 0.11.1v of typeahead. TIA

Upvotes: 3

Views: 2070

Answers (1)

vinhboy
vinhboy

Reputation: 8982

To accomplish what you want, you have to use two input fields.

// html
<input type="text" name="customer_typeahead" class="typeahead"/>
<input type="hidden" name="customer" id="customer_identity" />

// typeahead code
$('.typeahead').typeahead(null, {
  ....
}).bind('typeahead:select', function(ev, suggestion) {
  $('#company_identity').val(suggestion.identity);
});

This makes it so that when you select a suggestion, it will set the value of the customer input.

Upvotes: 1

Related Questions