The click function in jQuery is defined as .click( handler )
, so why can't I do the following:
var object1 = $("#object1");
var object2 = $("#object2");
object1.click(object2.toggle);
The following, however, works:
var object1 = $("#object1");
var object2 = $("#object2");
object1.click(function() { object2.toggle(); });
The question is, why do I have to encapsulate the function handler in another function in order for it to work? I know that in the first example, the toggle
function would be receiving all the events data, but it doesn't seem to affect (also, same happens with other functions such as show
, hide
, fadeIn
, etc)
UPDATE:
Regarding the arguments, this also seems to work:
var object1 = $("#object1");
var object2 = $("#object2");
object1.click(function(ev) { object2.toggle(ev); });
So, the invalid arguments don't seem to be a problem.
ANSWER:
The explanation of the problem is what @SeanWessell said, here's a JSFiddle showing the issue in a way I understood it better. It has nothing to do with jQuery though: https://jsfiddle.net/diegojancic/nffcnu8t/