Every time an event is triggered, all functions bound to the event on the specified target get executed. Return values for those callback functions are used to determine if the event should continue to propagate.
There are a few significant differences between a function invoked as part of a script and an event callback:
- jQuery normalizes the callback so that it executes in the context of the bound target (i.e.
this refers to the bound element that the event was assigned to)
- callback parameters are automatically provided
- events execute asynchronously when the event is fired, so there may be some unintended consequences with variable values (loop indices like
i may have their final value by the time the callback is called)
As callbacks are just functions, they can do anything that any other function can do, including calling trigger. Do be careful not to cause yourself an infinite loop with recursive function calls to the same event.
You wouldn't want to call:
function foo()
{
foo();
}
Likewise, you wouldn't want to call:
$('#foo').click(function(){
//short form for .trigger('click')
$(this).click();
});