🔥 $100K Hit! Where Will Bitcoin Go Next? Find Out Live!

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.