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
Post a Comment