How Do I Pass The Value (Not The Reference) of a JS Variable to a Function?

Posted by ryan on Stack Overflow See other posts from Stack Overflow or by ryan
Published on 2010-04-02T20:20:23Z Indexed on 2010/04/02 20:23 UTC
Read the original article Hit count: 232

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?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about listener