Click to See Complete Forum and Search --> : rewriting event handlers


palswim
01-17-2003, 01:39 PM
What would be another way to write this (that would work)?

"object.onclick = someFunction(parameters);"

Since I know you can't write it like that, but I don't know why event handlers have to be of form

"object.onclick = someOtherFunctionWithoutParametersOrParens;"

gil davis
01-17-2003, 08:35 PM
object.onclick = function (parameter) { // code goes here
}

For example:

document.onclick = function (evt) {
alert(evt.target.constructor + '; ' + evt.which);
}

palswim
01-18-2003, 03:13 AM
actually, i did code a solution earlier:
function setClick(obj,func){
eval("obj.onclick = function(){" + func + "};");
}