shishir mishra
shishir mishra

Reputation: 457

Which is right way to call two click event one after another?

I want to fire a click event on click to a button. I have written code below, both worked for me but I want to know which one is better way for same.

1st method:

 jQuery( "#responav li" ).click(function() {
     jQuery( "#close-icon" ).click();
    });

2nd method:

jQuery( "#responav li" ).click(function() { 
     jQuery( "#close-icon" ).trigger("click"); 
   });

Upvotes: 3

Views: 660

Answers (1)

Michael Coker
Michael Coker

Reputation: 53684

$.trigger('click') is a little more performant, since $.click() just runs $.trigger('click').

From https://api.jquery.com/click/

This method is a shortcut for [...] .trigger( "click" )

And from http://api.jquery.com/trigger/

Any event handlers attached with .on() or one of its shortcut methods are triggered when the corresponding event occurs. They can be fired manually, however, with the .trigger() method. A call to .trigger() executes the handlers in the same order they would be if the event were triggered naturally by the user.

Upvotes: 2

Related Questions