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 Coupon Found:

Verified STAFF PICK

TOP-RATED CODING COURSES

Learn the skills of tomorrow

The best time to learn programming is now - follow this link to access 100+ coding courses and enjoy learning at a very low cost!

Expiration date: 13/05/2021
2287 People Used
Only 92 Left
Rating
5.0