Just adding var wouldn't be enough, you're still referencing the variable from the outer scope. You need to create your own copy. Instead of:
els[i].addEventListener('click', function(){alert(i);});
Do: els[i].addEventListener('click',
function() {
var j = i;
alert(j);
});
Edit: This is wrong, archgoon has the correct answer below. That'll teach me for jumping on closure problems in the morning :)