Out of curiosity -- what is the purpose of / use cases for jQuery's triggerHandler
? As far as I can tell, the only "real" differences between trigger
and triggerHandler
is whether or not the native event fires, and event bubbling behavior (though triggerHandler
's bubbling behavior doesn't seem hard to replicate with trigger
in a few more lines of code). What is the advantage to ensuring the native event does not fire?
I'm curious if this is a convenience function or there's a deeper reason it exists, and what why/when I would use it.
For me the main difference is that 'triggerHandler' returns whatever was returned by the last handler, whereas 'trigger' returns the jQuery object.
So, for a handler such as:
Using 'triggerHandler' you can do the following:
So you would use 'triggerHandler' if you wanted to respond to the result returned from the handler.
From the Docs at http://api.jquery.com/triggerHandler/
Not preventing the default browser actions allow you to specify an action that occurs on focus or select, etc etc etc, that applies a style. Maybe you have a dynamic menu that is Javascript based, so you don't want to apply the style purely with CSS otherwise those with Javascript disabled won't understand why the layout looks odd. You can use something like
$('menu1select').triggerHandler('click');
If you have an event which hides an element onclick for example, and you want to call that function generally, instead of having to specify each element, you can use
$('.menu').triggerHandler('click')
;Prevents propagation, hopyfully don't have to explain this one...
This one should be self explanatory as well...
You have actions bound to a 'focus' event but you don't want the browser to focus really focus it (might seem dumb but it could happen, couldn't it? like a code that you would like to execute once without losing the current focus).
A component that you are making want to trigger 'load' (just an example of a generic thing) of another component that is inside it.
In that case, if you are calling 'load' of children when 'load' of the parent comes, you don't want to do this because it would cause an infinite call if the event.stopPropagation isn't called by the listeners of 'load' event (caused by bubling):
In that case you have to call triggerHandler().
Difference 1: you can call all elements matched by the JQuery object using trigger.
//Example1 for trigger. All 3 button click events are fired when used trigger. //Try Replacing trigger method with triggerHandler(). You will see only the first button element event handler will fire .
//substitute trigger with triggerHandler to see the difference
Difference 2: when using triggerHandler() for an element event, the native event will not be called for that element. trigger() will work fine.
//Example:
//substitute trigger with triggerHandler to see the difference
Difference 3: trigger() return Jquery object whereas triggerHandler() return the last handle value or If no handlers are triggered, it returns undefined
//Example3
Other difference is
Events triggered with triggerHandler() do not bubble up the DOM hierarchy; if they are not handled by the target element directly, they do nothing.