events - triggerHandler vs. trigger in jQuery -


out of curiosity -- purpose of / use cases jquery's triggerhandler? far can tell, "real" differences between trigger , triggerhandler whether or not native event fires, , event bubbling behavior (though triggerhandler's bubbling behavior doesn't seem hard replicate trigger in few more lines of code). advantage ensuring native event not fire?

i'm curious if convenience function or there's deeper reason exists, , why/when use it.

from docs @ http://api.jquery.com/triggerhandler/

the .triggerhandler() method behaves .trigger(), following exceptions:

  • the .triggerhandler() method not cause default behavior of event occur (such form submission).

not preventing default browser actions allow specify action occurs on focus or select, etc etc etc, applies style. maybe have dynamic menu javascript based, don't want apply style purely css otherwise javascript disabled won't understand why layout looks odd. can use $('menu1select').triggerhandler('click');

  • while .trigger() operate on elements matched jquery object, .triggerhandler() affects first matched element.

if have event hides element onclick example, , want call function generally, instead of having specify each element, can use $('.menu').triggerhandler('click');

  • events created .triggerhandler() not bubble dom hierarchy; if not handled target element directly, nothing.

prevents propagation, hopyfully don't have explain one...

  • instead of returning jquery object (to allow chaining), .triggerhandler() returns whatever value returned last handler caused executed. if no handlers triggered, returns undefined

this 1 should self explanatory well...


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -