Javascript Closure Problem
I know this kind of question gets asked alot, but I still haven't been able to find a way to make this work correctly. The code: function doStuff () { for (var i = 0; i< elementsList.length; i++) { elementsList[i].previousSibling.lastChild.addEventListener("click", function(){ toggle(elementsList[i])}, false); } } // ends function function toggle (element) { alert (element); } The problem is in passing variables to the toggle function. It works with the this keyword (but that sends a reference to the clicked item, which in this case is useless), but not with elementsList[i] which alerts as