cfair22 cfair22 - 1 month ago 14
jQuery Question

google places autocomplete suggestion trigger after X characters

I am attempting to limit the display of suggestions for the Google Place Autocomplete API.

Since the Google API charges a API Call per character entered in the field, I would like to restrict the suggestions to only be shown after X amount of characters have been entered.

My initialization of the autocomplete is as standard as it gets.

var input = document.getElementbyId('Searchbox');
var autocomplete = new google.maps.places.autocomplete(input)


Any tips or suggestions would be greatly appreciated

Thanks

Answer

Welcome to StackOverflow, cfair22, here we usually don't do the coding for you, but you can read the length of a string in javascript using length.

var input = document.getElementById("Searchbox");
if(input.length > 3)
{
    // ...
}

In this example, you can replace // ... with your code and it will only be fired if the input has length 4 or higher.

Also, I recommend that you cache results for as long as you like, to avoid useless charges.