as you have, all of your elements reference the same instance of currentOnClick . every time you assign currentOnClick current element of an existing onclick function, it loses the link to the previous function. you need to create a new event handler function in a separate area for each element.
function addClickProxy(element) { var currentOnClick = element.onclick; element.onclick = function() { if (currentOnClick) { currentOnClick(); } alert("hello2"); } } window.onload = function() { for (var i = 0; i < document.forms[0].elements.length; i++) { addClickProxy(document.forms[0].elements[i]); } }
Thus, there are 3 different instances of currentOnClick floating around, each of which is closed from the scope of the function.
source share