Skip to content

Instantly share code, notes, and snippets.

@ppeeou
Created April 7, 2017 07:43
Show Gist options
  • Save ppeeou/6ed9738507963f1e9acd936dc7277e9e to your computer and use it in GitHub Desktop.
Save ppeeou/6ed9738507963f1e9acd936dc7277e9e to your computer and use it in GitHub Desktop.
js Handler event
<!DOCTYPE>
<html>
<head>
</head>
<body>
<div id="div1">div1</div>
<div id="div2">div2</div>
<div id="div3">div3</div>
<div id="div4">div4</div>
<div id="div5">div5</div>
<script>
var div1 = document.getElementById('div1');
var div2 = document.getElementById('div2');
var div3 = document.getElementById('div3');
var div4 = document.getElementById('div4');
var div5 = document.getElementById('div5');
var arr = [div1, div2, div3, div4, div5];
var add_the_handler = (function (nodes) {
var i;
for (i = 0; i < nodes.length; i++) {
nodes[i].onclick = (function (i) {
return function (e) {
alert(i);
}
})(i);
}
})(arr);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment