Last active
July 23, 2018 19:22
-
-
Save augbog/16ec15a9647db5a997ceb573268422de to your computer and use it in GitHub Desktop.
Basic interview question on closures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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