Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// ye olde ie8-compatible way
function setupTheThing(thing, i) {
thing.addEventListener("click", function() { console.log(i); });
}
var i, thing;
for (i = 0; i < 5; i++) {
thing = document.getElementById("thing" + i);
setupTheThing(thing, i);
}
// yer shiny new JS2015 way
for (i = 0; i < 5; i++) {
let ii = i; // "let" is a 'private' variable
let thing = document.getElementById("thing" + ii);
thing.addEventListener("click", function() { console.log(ii); });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.