I’m writing a code to autocomplete the city search on my site. The problem is that the ajax function, once called, blocks subsequent calls to the keyup event. In practice, as soon as three characters are reached in the input, the ajax call takes the data from the API I am using (all fine in this point) but then something happens, and the keyup event is not callable again (my console.log is not fired).
this is my HTML code:
<input type="text" id="prova" class="form-control search-slt" placeholder="Enter name1..." data-behavior="autocomplete" />
this is my JS code:
$(function () {
var minlength = 3;
$('#prova').keyup(function (e) {
var that = this,
value = $(this).val();
console.log("keyup");
if (value.length >= minlength ) {
searchRequest = $.ajax({
type: "GET",
url: "https://nominatim.openstreetmap.org/search?q=" + $("#prova").val() + "&format=json",
//async: false,
success: function(results) {
console.log(results);
var aList = results;
var aOptions = [];
for (i = 0; i < aList.length; i++) {
//optKey = aList[i].geometry.coordinates[0] + ',' + aList[i].geometry.coordinates[1];
optLabel = aList[i].display_name;
aOptions.push(optLabel);
}
var options = {
data: aOptions
};
console.log(options);
$("#prova").easyAutocomplete(options);
}
});
}
});
});
I debugged the code and I realized that the easyAutocomplete
function is the one that blocks subsequent keyup calls. Something happens that I don’t understand. easyAutocomplete
is a Rails gem but it is nothing more than a wrapper for the jQuery’s easyAutocomplete plugin. Can you tell me what happens when I pass my options
array to the easyAutocomplete
function?
2
Answers
You may be missing the libraries. I got it working with your JavaScript:
I made a folder called lookup. In the folder I made two files:
test.html
lookup.js
That’s it. Should work fine. Hope this helped you.
OK. Try this. Change the lookup.js to be just the following:
Now should work the way you want. Please click on the up arrows to the answers if they helped you. That helps my ranking. Also, If it fixed it for you please click on the "This fixed my issue".
Thanks,
Jeremy