tags:

views:

27

answers:

3

I'm embarrassed to even ask BUT could someone help me understand what a "handler" is. I am new to jQuery and the API constantly has references similar to the following:

toggle( handler(eventObject), handler(eventObject), [ handler(eventObject) ] )

I scratch my head and say to myself "what the hell is a handler". Then I check my 2 jquery books and don't really see anything specific there. I get what an event handler does, it handles an event. But the word handler in the above context confuses me including "eventObject". I tried to google it but could not really find a really clear definition of what exactly a handler is as it relates to jquery. Thanks for your help =]

+1  A: 

This is the function which will handle the event. To expand, in the case of toggle, ON calls the first function (with the eventObject) and OFF calls the second function. eventObject will hold different info depending on events, like coordinates of the mouse.

aepheus
+2  A: 

Handlers are any functions that you write to handle events. For e.g. in

$(document).ready(function() {
      //......
});

the handler is

function() {
     //.......
}
rajasaur
Thanks rajasaur, that was quick.
chainwork
+2  A: 

Think of a handler as a callback for whatever operation is being invoked. In the case of handler(eventObject) it means that the method with that parameter can accept a function being passed to it and that function will be called at some specific point in time before, during, or after the execution of the method receiving it (as indicated by the parameter specification) and it will be passed a value called eventObject which can be anything, but is most likely the target of the given event your callback is being issued for.

Here's an example:

function MyCallback(eventObject) {
    alert(jQuery(eventObject).attr('id') + ' toggled'));
}

jQuery("#myBtn").click(function() {
    jQuery("#myObj").toggle("fast", function(eventObject) { MyCallback(eventObject); });
});

With the above code, when #myBtn is clicked the element #myObj will be toggled (fast) and as soon as the toggle animation completes MyCallback will be called and passed #myObj which will cause an alert to appear saying, "myObj toggled".

Nathan Taylor
You guys make me love stackoverflow so very much.
chainwork
Glad to help! :)
Nathan Taylor