0

If you want to click a link with jQuery, you can use one of the following methods:

$('a').click();

$('a').trigger('click');


Which is better? (performance, browser support, i.e.)

tolborg
  • 612
  • 4
  • 21

3 Answers3

1

There seems to be none, performance wise.

See: http://forum.jquery.com/topic/a-trigger-click-vs-a-click

This method is a shortcut for .bind('click', handler) in the first variation, and.trigger('click') in the second.

Except you can extend the trigger command.


Seems like i was mistaking.

Since click is actually calling trigger, if no function is called. See: jQuery advantages/differences in .trigger() vs .click()

And for performace results, @VisioN linked to this: http://jsperf.com/click-vs-trigger-click

So, basicly using trigger is the fastest way, also i think it actually tells what you are doing, instead of just doing it.

Community
  • 1
  • 1
Marco Johannesen
  • 13,084
  • 6
  • 31
  • 36
0

http://forum.jquery.com/topic/a-trigger-click-vs-a-click

In this form they are the same. As the api reference states:

This method is a shortcut for .bind('click', handler) in the first variation, and .trigger('click') in the second.

The second can also be used to attach a function to the event.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
LiamB
  • 18,243
  • 19
  • 75
  • 116
-2

Exactly the same. But I prefer $('a').bind('click', function(){});

Pulkit Mittal
  • 5,916
  • 5
  • 21
  • 28