google maps - simple javascript for loop not behaving the way I think it should -


for (var i=0; i<gmaps.map.markers.length; i++) {   google.maps.event.addlistener(gmaps.map.markers[i].serviceobject, 'click', function(object){     alert(gmaps.map.markers[i]);    }); }; 

so, goes through loop. i'm using google maps api, obviously. each of markers on map has alert attached it. alert shows undefined though. if switch alert(gmaps.map.markers[0]); or other applicable number, shows me object object, should. if call alert(gmaps.map.markers[i].id); or like, value i'm looking for, obviously, every marker gives same alert.

why i not being recognized in callback?

you have privatize i:

for (var i=0; i<gmaps.map.markers.length; i++) {  (function(i){   google.maps.event.addlistener(gmaps.map.markers[i].serviceobject, 'click', function(object){     alert(gmaps.map.markers[i]);    });  }(i)); }; 

Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -