No way to remove google places autocomplete?

thelolcat picture thelolcat · Oct 10, 2015 · Viewed 11.1k times · Source
var autocomplete = new google.maps.places.Autocomplete(input, {
  types:  ['geocode']
});

How do I remove it now?

There's "autocomplete.unbindAll()" function but it doesn't remove the HTML for the dropdown box. I have a page that does a lot of ajaxy stuff and because of this dropdown boxes keep being added even tho there's only one autocomplete at a given time on the page :|

Answer

Prabhu picture Prabhu · Nov 7, 2015

Assuming this is how things have been initialised

var autocomplete = new google.maps.places.Autocomplete(input, {types: ['geocode']});
var autocompleteLsr = google.maps.event.addListener(autocomplete, 'place_changed', function() { ... });

google maps APIs also provide removeListener and clearInstanceListeners for removal as well. https://developers.google.com/maps/documentation/javascript/reference#event

also as an optional step, you should remove this $(".pac-container") as well

google.maps.event.removeListener(autocompleteLsr);
google.maps.event.clearInstanceListeners(autocomplete);
$(".pac-container").remove();