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

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

1

yes .click() can trigger click event

you can do it like this

$('element').click();

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.