Also, I would like to add few more things.
First, think of onclick as an ordinary property of an object.
So, object.onclick = value where value = functionName() is
perfectly alright, for example, functionName() could be returning a value
using return something;
But onclick is not an ordinary property. When JS engine in browser
encounters assignments to event based properties (e.g. onclick,ondblclick,onmouseover, etc.), it creates a stack for each
element, consisting of mapping b/w
onSomeEvent and handler.
The handler is a function object. But if you do something like
element.onSomeEvent = functionName(); you are simply calling the
function functionName() and this would have been alright if
functionName() was defined as
function functionName() {return function(){/*do something*/};}
But because functionName in your case is not returning a function, you see an unexpected result. This is the major
drawback of languages like JavaScript. In a strongly typed language that would have
thrown an error... because
class Element {
Function onclick = null;
Function ondblclick = null;
/*other code*/
}
document.getElementById('idName').onclick = new Function(); // correct
document.getElementById('idName').onclick = function(){}; // correct
document.getElementById('idName').onclick = functionName();
/*
* correct if functionName defined as
* Function functionName() {return new Function();} or
* Function functionName() {return function(){};}
* and incorrect for any other case
*/
()? Remove them and you're fine.