Last active
July 23, 2018 19:22
Star
You must be signed in to star a gist
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