jQuery trigger: Main Tips
- The jQuery
.trigger()
method runs all event handlers assigned to elements for the provided event type. - .on() and its shortcut methods run when events occur naturally. The
.trigger()
runs handlers manually in their regular sequence.
Usage of .trigger()
The jQuery .trigger()
method triggers the specified event handler on matched elements.
Example
$("input").select(() => {
$("input").after("Hey");
});
$("button").click(() => {
$("input").trigger("select");
});
Unlike the .triggerHandler() method, which only triggers the handler, .trigger()
in jQuery includes both the default event behavior and the handler functions.
The syntax for this method:
$("selector").trigger(eventType, [parameter1], [parameter2], ..., [parameterN]);
The jQuery .trigger()
method takes two parameters:
eventType
specifies a standard JavaScript event or a customjQuery.event
object.parameter
defines the parameters you may pass to the event handler function.