0

Does the jQuery .click method trigger the .click event handler?

And if not is there a way to trigger the event handler (basically, on my page, links can be clicked on by the mouse or triggered by my javascript and I want to handle what happens for both these cases in the same way - i.e handle all within the click event handler).

spiderplant0
  • 3,710
  • 11
  • 48
  • 86

3 Answers3

1

yes .click() can trigger click event

you can do it like this

$('element').click();
Tushar Gupta - curioustushar
  • 56,454
  • 22
  • 99
  • 107
1

You could also try: .trigger. Like this:

$('#element').trigger("click");

You could also pass additional parameters to it:

$('#element').trigger("click",parameter);

Your event handler looks like this:

$('#element').on('click', function(event, parameter) {

});

Some opinions think that trigger("click") is better than .click. Check out this discussion and this discussion for the differences between .click and .trigger("click")

Community
  • 1
  • 1
Khanh TO
  • 47,821
  • 12
  • 98
  • 113
0

The documentation for .click() says that calling it without arguments is a shortcut for .trigger( "click" ).

The documentation for .trigger() says:

Description: Execute all handlers and behaviors attached to the matched elements for the given event type.

Taken together, this clearly says that calling .click() will trigger the click handlers.

Barmar
  • 669,327
  • 51
  • 454
  • 560