101 of 119 menu

The triggerHandler method

The triggerHandler method allows you to run all event handlers attached to an element. It works similarly to the trigger method, but there are some differences between them: The event handler will be fired, but the standard event will not be called. For example, triggerHandler('submit'), having been fired, will not call submit. The triggerHandler method only acts on the first element in the set. Events fired by triggerHandler do not bubble up in the DOM element hierarchy. triggerHandler does not return a jQuery object, it returns the value received by the last handler called. If no handlers are triggered, it returns undefined.

Syntax

As the first parameter we can pass the event type as a string, the second optional array or object containing additional parameters to pass to the handler:

$(selector).triggerHandler(event type, [additional parameters]);

You can pass an event as the first parameter in the form of a jQuery.Event object:

$(selector).trigger(event, [additional parameters]);

Example

Let's make it so that when we click the button with #test1, our input will get focus and 'Focused' will be printed in the console, in other words, both the handler function and the focus event will be triggered. When we click the second button with #test2, only the handler function will be triggered, but the input will not get focus:

<button id="test1">button1</button> <button id="test2">button2</button> <input type="text"> $('#test1').click(function() { $('input').trigger('focus'); }); $('#test2').click(function() { $('input').triggerHandler('focus'); }); $('input').focus(function() { console.log('Focused') });

See also

  • method trigger,
    which allows you to run all event handlers attached to an element for events of a given type
  • method on,
    which allows you to bind an event handler to an element
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline