I'm using jQuery Bootstrap Typeahead. I get data from my file but typeahead dropdown is not populate/shown.
Here is my JS:
(function() {
"use strict";
var $input = $('.typeahead');
$('.typeahead').typeahead({
source: function (query, process) {
return $.getJSON(
'url-to-file.php',
{ query: query },
function (data) {
console.log(data)
return process(data);
})
}
});
})();
console.log()
returns a (correct) JSON array:
[ "item1", "item2", ... ]
But I don't know what to do after. Thanks for helping
The data source should be...
[{name:"item1","id":"1"},{name:"item2","id":"2"},etc..]
And then use data-provide
on an input like this...
<div class="input-group">
<input type="text" data-provide="typeahead" class="form-control typeahead border-primary" name="query" id="query" placeholder="search..." autocomplete="off">
</div>
Working Demo on Codeply
Edit: You can also use a simple string array: https://www.codeply.com/go/knBpcbhlFE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With