Skip to content

Instantly share code, notes, and snippets.

@psiborg
Created January 6, 2012 08:13
Show Gist options
  • Save psiborg/1569634 to your computer and use it in GitHub Desktop.
Save psiborg/1569634 to your computer and use it in GitHub Desktop.
JS Closure
for (var i = 0; i < 10; i++) {
var link = document.createElement("a");
link.innerHTML = "Link " + i;
link.href = "#";
link.onclick = (function (num) {
return function () {
alert("This is link " + num);
return false;
};
}(i));
document.body.appendChild(link);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment