This question has already been answered:
- How short circuits work in JavaScript 6 responses
var add_the_hendlers = function(node) { var i; for(i = 0; i < node.length; i++) { node[i].onclick = function() { console.log(i) } } } The problem is that console.log always returns the last increment, that is, on the page for example 3 diva, and when you click on them, 3 is always displayed, I know that you need to use closures, they say there is a reference to the variable, but why in this case so?