Skip to content

Instantly share code, notes, and snippets.

@augbog
Last active July 23, 2018 19:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save augbog/16ec15a9647db5a997ceb573268422de to your computer and use it in GitHub Desktop.
Basic interview question on closures
<div id="hello1">Hello1</div>
<div id="hello2">Hello2</div>
<div id="hello3">Hello3</div>
<div id="hello4">Hello4</div>
<div id="hello5">Hello5</div>
window.onload = (function(e) {
for (var i=1; i<=5; i++) {
var el = document.getElementById('hello' + i);
el.addEventListener('click', function(e) {
alert(i);
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment