How Do I Pass The Value (Not The Reference) of a JS Variable to a Function?
- by ryan
Here is a simplified version of something I'm trying to run:
for (var i = 0; i < results.length; i++) {
marker = results[i];
google.maps.event.addListener(marker, 'click', function() {
change_selection(i);
});
}
but I'm finding that every listener uses the value of results.length (the value when the for loop terminates). How can I add listeners such that each uses the value of i at the time I add it, rather than the reference to i?