.triggerHandler( eventType [, extraParameters ] )Returns: Object
Description: Execute all handlers attached to an element for an event.
-
version added: 1.2.triggerHandler( eventType [, extraParameters ] )
- eventTypeType: StringA string containing a JavaScript event type, such as
click
orsubmit
. - extraParametersType: Array or PlainObjectAdditional parameters to pass along to the event handler.
-
-
version added: 1.3.triggerHandler( event [, extraParameters ] )
- eventType: EventA
jQuery.Event
object. - extraParametersType: Array or PlainObjectAdditional parameters to pass along to the event handler.
-
.triggerHandler( eventType )
executes all handlers bound with jQuery for the event type. It will also execute any method called on{eventType}()
found on the element. The behavior of this method is similar to .trigger()
, with the following exceptions:
- The
.triggerHandler( "event" )
method will not call.event()
on the element it is triggered on. This means.triggerHandler( "submit" )
on a form will not call.submit()
on the form. - While
.trigger()
will operate on all elements matched by the jQuery object,.triggerHandler()
only affects the first matched element. - Events triggered with
.triggerHandler()
do not bubble up the DOM hierarchy; if they are not handled by the target element directly, they do nothing. - 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 returnsundefined
For more information on this method, see the discussion for .trigger()
.
Example:
If you called .triggerHandler() on a focus event - the browser's default focus action would not be triggered, only the event handlers bound to the focus event.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>triggerHandler demo</title> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <button id="old">.trigger( "focus" )</button> <button id="new">.triggerHandler( "focus" )</button><br><br> <input type="text" value="To Be Focused"> <script> $( "#old" ).click(function() { $( "input" ).trigger( "focus" ); }); $( "#new" ).click(function() { $( "input" ).triggerHandler( "focus" ); }); $( "input" ).focus(function() { $( "<span>Focused!</span>" ).appendTo( "body" ).fadeOut( 1000 ); }); </script> </body> </html>