Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery trigger() method

jQuery trigger() method

jQuery event methods jQuery event methods

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:

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 jQuery event methods