0

In a text search field if I select a autocomplete option from google maps autocomplete, the maps autocomplete listener gets triggered and called. But it also gets called if I press return/enter on my forms text search field.

I have a jquery on keydown event below that I want to perform some additional work before I submit ex. get location info for the searched text, but I want to know if the listener will ever get called before or during the on keydown code?

$("#navbarSearchField").off('keydown').on('keydown', function (event) {
  // do stuff here
  
});

var navbarSearchAutocomplete = new google.maps.places.Autocomplete(location, {
  types: ['geocode']
});
google.maps.event.addListener(navbarSearchAutocomplete, 'place_changed', function() {
  // do some work here

});

It seems like from the testing I've done, the maps listener gets called after my last line of on keydown code but how can I test to be sure?

chuckd
  • 13,460
  • 29
  • 152
  • 331
  • 1
    I am not sure if I understand your question correctly? But the place_changed is triggered as soon as a user selected/clicked a place not? So I am not sure what you want to know haha. – Rens Tillmann Mar 06 '18 at 04:14
  • If I understand correct, answer should be: No, the place_changed listener will not be called when keydown is triggered, only when a user actively chooses a place/location. At least that's my experience so far. – Rens Tillmann Mar 06 '18 at 04:16
  • it does get triggered if the keydown is 13 (enter) – chuckd Mar 06 '18 at 04:19
  • maybe it's because I have it in a form and the enter button is submitting the form?? – chuckd Mar 06 '18 at 04:25
  • Hmmm could be, let me test on my end if "Enter" does the same thing. – Rens Tillmann Mar 06 '18 at 04:30
  • For me it did not trigger when "Enter" is clicked. (it did not select a place). And therefore not trigger `place_changed` – Rens Tillmann Mar 06 '18 at 04:34
  • well, those are two differnet things. When I press enter it gets triggered, BUT there is no location found. So I do a a check like this var searchedPlace = navbarSearchAutocomplete.getPlace(); – chuckd Mar 06 '18 at 04:36
  • if place is empty I say ok it was from a enter being pressed and not a autocomplete item clicked – chuckd Mar 06 '18 at 04:36
  • Yes that's a good way to determine, but was the reason no location is found is because it is not triggered on "Enter" press. So not sure why it get's triggered, it must be because it listens to form being submitted (but i do not know this). – Rens Tillmann Mar 06 '18 at 04:39
  • that's probably the reason – chuckd Mar 06 '18 at 04:40

0 Answers0