jQuery trigger() method
Example
Triggers The select event of the element:
$
(
"
button
"
)
.
click
(
function
(
)
{
$
(
"
input
"
)
.
trigger
(
"
select
"
)
;
}
)
;
Try it »
Definition and Usage
The trigger() method fires the specified event on the selected elements, along with the event's default behavior (such as form submission).
This method is similar to the triggerHandler() The methods are similar, except that triggerHandler() does not fire the event's default behavior.
Differences compared with the triggerHandler() method:
- The trigger() method 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 »
If you want to trigger a link's default behavior, such as navigating to a specified URL, you need to call the event.preventDefault() method inside the event handler, for example:
Example
$
(
'
#myLink
'
)
.
on
(
'
click
'
,
function
(
event
)
{
event
.
preventDefault
(
)
;
window
.
location
.
href
=
'
https://www.runoob.com
'
;
}
)
;
Try it »
Syntax
$(
selector
).trigger(
event,eventObj,param1,param2,...
)
| Parameters | Description |
|---|---|
| event | Required. Specifies the event to trigger on the given element. |
| It can be a custom event or any standard event. | |
| 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