Skip to content Skip to sidebar Skip to footer

Passing Google Event Data In Addlistener

I have a variable number of text fields to which I am initialising Google Maps Autocomplete dynamically. The problem is that every time the place_changed event is fired, I need to

Solution 1:

Possible approach:

var initAutocomplete = function (element) {
  var autocomplete = new google.maps.places.Autocomplete(element);
  google.maps.event.addListener(autocomplete,
                                'place_changed', 
                                function(){saveLocation.call(autocomplete,element)});
},

saveLocation = function (element) {
        document.getElementsByTagName('code')[0]
          .innerHTML=element.getAttribute('name');
        var place = this.getPlace();
        
};

google.maps.event.addDomListener(window, 'load', function(){
  var inputs=document.querySelectorAll('.pac');
  for(var i=0;i<inputs.length;++i){
    initAutocomplete(inputs.item(i));
  }
});
<code></code><br/><inputclass="pac"name="field1">field1<br/><inputclass="pac"name="field2">field2<br/><inputclass="pac"name="field3">field3<br/><scriptsrc="https://maps.googleapis.com/maps/api/js?v=3&libraries=places"></script>

Post a Comment for "Passing Google Event Data In Addlistener"