jQuery triggerHandler: Main Tips
- The jQuery
.triggerHandler()
method runs handlers assigned to an element for an event. - .trigger() executes handlers on all detected elements. The jQuery
.triggerHandler()
influences the first matched element.
How .triggerHandler() Work
The .triggerHandler()
lets you trigger jQuery handler of the specified event on the selected element.
Example
$("button").click(() => {
$("input").triggerHandler("select");
});
The syntax for jQuery .triggerHandler()
is similar to trigger jQuery method:
$("selector").triggerHandler(event, [parameter1], [parameter2], ..., [parameterN]);
This method takes two parameters:
event
represents a standard JavaScript event or a customjQuery.event
object.parameter
means you may also add multiple parameters to be passed to the event handler function.
Note: unlike the .trigger() jQuery method, .triggerHandler() includes the handler function only, and not the default event behavior.