views:

46

answers:

1

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.

+1  A: 

From the Docs at http://api.jquery.com/triggerHandler/

The .triggerHandler() method behaves similarly to .trigger(), with the following exceptions:

  • The .triggerHandler() method does not cause the default behavior of an event to occur (such as a form submission).

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');

  • While .trigger() will operate on all elements matched by the jQuery object, .triggerHandler() only affects the first matched element.

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');

  • Events created with .triggerHandler() do not bubble up the DOM hierarchy; if they are not handled by the target element directly, they do nothing.

Prevents propagation, hopyfully don't have to explain this one...

  • Instead of returning the jQuery object (to allow chaining), .triggerHandler() returns whatever value was returned by the last handler it caused to be executed. If no handlers are triggered, it returns undefined

This one should be self explanatory as well...

Robert
The docs are why I asked the question in the first place. I understand the behavior, but I'm curious if this is a convenience function or there's a deeper reason it exists.
David Eads
You seriously downvoted because you don't understand the examples given in the docs. Alright, let me spell it out for you then.
Robert
I upvoted you for giving it a shot, but I don't think your answer is adequate by any means, so I might downvote it again. Copy and pasting the docs is lazy and I clearly already read the docs. #1 In your first example, if you bind to the click event and return false, it accomplishes the same thing. #2 In your second example, I think you are just wrong -- triggerHandler ONLY applies to the first element.#3: propogation can also be stopped with event.stopPropogation.#4: Self explanatory.What I want to understand is why/when I'd use this instead of regular trigger.
David Eads
You don't have to upvote, you can just not vote at all... I gave you more examples and you still don't understand, which isn't my fault. In some cases you don't want to return false on an actual click, so #1 is valid. #2 Yeah... that's the point, hiding only one, but calling the entire matching array. So you can setInterval that function to increment hiding #3 See #1, with the idea of you wanting different actions based on actual click vs simulated. #4 If you want the value returned by the last actual event without creating a new one.
Robert