jQuery triggerHandler() method
Example
Triggers The select event of the element:
$
(
"
button
"
)
.
click
(
function
(
)
{
$
(
"
input
"
)
.
triggerHandler
(
"
select
"
)
;
}
)
;
Try it »
Definition and Usage
The triggerHandler() method fires the specified event on the selected elements.
The return value of this method is the return value of the event handler, rather than a chainable jQuery object. In addition, if no handler is triggered, this method returns undefined.
This method is similar to the trigger() The methods are similar, except that trigger() also fires the event's default behavior (such as form submission).
Differences compared with the trigger() method:
- It does not cause the event's default behavior (such as form submission)
- .trigger() operates on all elements matched by the jQuery object, while .triggerHandler() affects only the first matched element.
- Events created by .triggerHandler() do not bubble up the DOM tree; if the target element does not handle them directly, nothing happens.
Example
Comparing triggerHandler() with .trigger():
$
(
"
#old
"
)
.
click
(
function
(
)
{
$
(
"
input
"
)
.
trigger
(
"
focus
"
)
;
}
)
; $
(
"
#new
"
)
.
click
(
function
(
)
{
$
(
"
input
"
)
.
triggerHandler
(
"
focus
"
)
;
}
)
; $
(
"
input
"
)
.
focus
(
function
(
)
{
$
(
"
Focused!
"
)
.
appendTo
(
"
body
"
)
.
fadeOut
(
1000
)
;
}
)
;
Try it »
Syntax
$(
selector
).triggerHandler(
event,param1,param2,...
)
| Parameters | Description |
|---|---|
| event | Required. Specifies the event to trigger on the given element. |
| param1,param2,... | Optional. Extra arguments to pass to the event handler. |
| Extra arguments are especially useful for custom events. |
More examples
Passing extra arguments to a custom event
How to pass extra arguments to a custom event handler.
The difference between trigger() and triggerHandler()
Demonstrates the difference between trigger() and triggerHandler().
jQuery event methods