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).

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
spiderplant0
  • 3,872
  • 12
  • 52
  • 91

3 Answers3

1

yes .click() can trigger click event

you can do it like this

$('element').click();
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 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
  • 48,509
  • 13
  • 99
  • 115
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
  • 741,623
  • 53
  • 500
  • 612