Code has been added to clipboard!

Learning to Make jQuery Trigger Specified Events in Minutes

Reading time 1 min
Published Jan 10, 2018
Updated Oct 2, 2019

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 custom jQuery.event object.
  • parameter defines the parameters you may pass to the event handler function.

Latest Udacity Coupon Found:

Verified STAFF PICK

75% OFF COURSES

Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 28/01/2021
3176 People Used
Only 97 Left
Rating
5.0