Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does jQuery .on("click") always run before <a> href fires?

Tags:

$("a").on("click", function (e) {     doSomething(); }); ... <a href="http://website.com">My Link</a> 

Will doSomething() always run before the "href", in every browser?

like image 294
D.Tate Avatar asked Apr 25 '13 16:04

D.Tate


People also ask

What is the difference between .click and .on (' click in jQuery?

So onclick creates an attribute within the binded HTML tag, using a string which is linked to a function. Whereas . click binds the function itself to the property element.

How do you trigger a click event for a hyperlink?

Answer: Use the jQuery click() Method You can use the click() method to trigger a click on a link programmatically using jQuery.

Why is this jQuery Ajax click event firing multiple times?

This happens because somewhere in your code, you're rebinding the event handler without first unbinding it.

How do you trigger a click event?

Trigger Click Event in JavaScript Using click() An element receives the click event when pressed, and a key is released on the pointing device (eg, the left mouse button) while the pointer is within the element. click() is triggered after the down and up mouse events are triggered in that order.


1 Answers

Yes, your handler will run always first. That's what allows you, for instance, to cancel default behavior (navigate to href url) if necessary

$("a").on("click", function (e) {    e.preventDefault(); // --> if this handle didn't run first, this wouldn't work    doSomething(); }); 
like image 85
Claudio Redi Avatar answered Sep 22 '22 14:09

Claudio Redi