function myTestFunction() { alert('myTestFunction() invoked!'); } window.myTestFunction = myTestFunction; var elements = document.getElementsByTagName("input"); for (var i = 0 ; i < elements.length ; i++) elements[i].setAttribute("onclick", "window.myTestFunction()");