javascript - assign typeahead.js array elements to variables -


i have typeahead tied <input> box , works fine. send request off server gives array 3 elements. dropdown box displays, , displaykey tells input box display out user.

the thing is, can grab value user has selected after fact using

var = $('#inputbox').val(); or something, need 3 array elements (partnumber, mfr, title) returned separate variables, , i'm not sure in function can assign those.

// bloodhound var partslist = new bloodhound({     datumtokenizer: function (datum) {         return bloodhound.tokenizers.whitespace(datum.value);     },     querytokenizer: bloodhound.tokenizers.whitespace,     remote: {         url: 'psearch.php?p=%query',                         wildcard: '%query',         filter: function (partslist) {                     return $.map(partslist, function (itemnumber) {                             return {                     partnumber:   itemnumber.pn,                     mfr:          itemnumber.mfr,                     title:        itemnumber.neda                                    };             });         }     } });   partslist.initialize(); // typeahead $('.typeahead').typeahead(null, {     displaykey: 'partnumber',         limit: 9,     source: partslist.ttadapter(),     templates: {         suggestion: handlebars.compile("<div><strong>{{partnumber}}</strong> > <div class='subtitle'>{{mfr}} > {{title}}</div></div>")     }     }); 

i searched around , found solution using .on('typeahead:selected'

$('.typeahead').typeahead(null, {     displaykey: 'partnumber',         templates: {         suggestion: handlebars.compile("<div><strong>{{partnumber}}</strong> > <div class='mfr_suggestion'>{{mfr}} > {{neda}}</div></div>")     },         source: partslist.ttadapter(),     limit: 9}).on('typeahead:selected', function (obj, datum) {         console.log(datum.partnumber);     console.log(datum.mfr);     console.log(datum.title); }); 

the 3 variables passed bloodhound can accessed name using datum.[name] convention.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -