.trigger( eventType [, extraParameters ] ) Returns: jQuery
Description: Execute all handlers and behaviors attached to the matched elements for the given event type.
-
version added: 1.0.trigger( 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.trigger( event )
-
eventType: EventA
jQuery.Event
object.
-
Any event handlers attached with .on()
or one of its shortcut methods are triggered when the corresponding event occurs. They can be fired manually, however, with the .trigger()
method. A call to .trigger()
executes the handlers in the same order they would be if the event were triggered naturally by the user:
1
2
3
4
|
|
As of jQuery 1.3, .trigger()
ed events bubble up the DOM tree; an event handler can stop the bubbling by returning false
from the handler or calling the .stopPropagation()
method on the event object passed into the event. Although .trigger()
simulates an event activation, complete with a synthesized event object, it does not perfectly replicate a naturally-occurring event.
To trigger handlers bound via jQuery without also triggering the native event, use .triggerHandler()
instead.
When we define a custom event type using the .on()
method, the second argument to .trigger()
can become useful. For example, suppose we have bound a handler for the custom
event to our element instead of the built-in click
event as we did above:
1
2
3
4
|
|
The event object is always passed as the first parameter to an event handler, but if additional parameters are specified during a .trigger()
call, these parameters will be passed along to the handler as well. To pass more than one parameter, use an array as shown here. As of jQuery 1.6.2, a single parameter can be passed without using an array.
Note the difference between the extra parameters we're passing here and the eventData
parameter to the .on() method. Both are mechanisms for passing information to an event handler, but the extraParameters
argument to .trigger()
allows information to be determined at the time the event is triggered, while the eventData
argument to .on()
requires the information to be already computed at the time the handler is bound.
The .trigger()
method can be used on jQuery collections that wrap plain JavaScript objects similar to a pub/sub mechanism; any event handlers bound to the object will be called when the event is triggered.
Note: For both plain objects and DOM objects, if a triggered event name matches the name of a property on the object, jQuery will attempt to invoke the property as a method if no event handler callsevent.preventDefault()
. If this behavior is not desired, use.triggerHandler()
instead.
Examples:
Example: Clicks to button #2 also trigger a click for button #1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
|
Example: To submit the first form without using the submit() function, try:
1
|
|
Example: To submit the first form without using the submit() function, try:
1
2
3
4
5
|
|
Example: To pass arbitrary data to an event:
1
2
3
4
5
|
|
Example: To pass arbitrary data through an event object:
1
2
3
4
|
|
Example: Alternative way to pass data through an event object:
1
2
3
4
5
6
|
|