2

I have a problem with jQuery preventDefault from running the onclick function of an tag. Here is my example: Avoid page jump on # click

$("#main a").bind("click", function (e) { 
    e.preventDefault();
}); 

I thought preventDefault would stop the default event (href="#"). But that it would let the "onclick" event stil fire. If I put the function inside my function it works. But I want to have different onclick functions on the links.

7
  • 2
    This isn't 1999, get rid of the inline event handlers, problem solved! Commented Feb 24, 2015 at 11:04
  • adeneo is right, you bind two clicks to a single element. One using inline javascript onclick="..." and the other with jQuery $("#main a").bind("click"..., just remove the inline one. Commented Feb 24, 2015 at 11:07
  • You can bind the click using jquery or plain js. (same @adeneo and @Jeremy). To check some particular behavior (depending the link that the users are coming from) after the event, I would do it inside the function that the event is handled. Commented Feb 24, 2015 at 11:13
  • @downvoter: The question may have been newbish, but it was in no way a bad question. He followed the "How to ask" rules and the question itself was posed correctly. It did not deserve a downvote imo. Commented Feb 24, 2015 at 11:35
  • Thanks everyone! This was some code I was cleaning up on (not mine). I would not myself write an inline onclick today after all I know about jQuery :-) But now I understand there were 2 click bindings on the element. I really was wondering because I got this to work on an <area> tag with an "onclick" function. They wanted me to add the preventDefault to avoid the page jumping on clicks. It was strange that it worked there. Here there is an "onclick" function on the <area> and I added a bind function to the area also. Seems to work here: uu-k1.no/kart_v2015 Commented Feb 24, 2015 at 12:43

1 Answer 1

2

As adeneo and Jeremy have mentioned, do not use inline event handlers. They will interfer with your jQuery event handler.

Simply merge the 2 functions into one, and it will work:

$("#main a").bind("click", function (e) { 
    e.preventDefault();

    var p = $(this).position();
    $("#loader").css("top", p.top - 20);
    $("#loader").toggle();
});

JSFiddle


If I put the function inside my function it works. But I want to have different onclick functions on the links.

If you want to have different onclick functions on different links, then use classes (or IDs) and select them appropriately with jQuery.

HTML:

<div id="main">
    <p><a class="doSomething" href="#">I will NOT JUMP!</a></p>
    <p><a class="doSomethingElse" href="#">I will NOT JUMP</a></p>
</div>

jQuery:

$("#main a.doSomething").bind("click", function (e) { 
    e.preventDefault();

    var p = $(this).position();
    $("#loader").css("top", p.top - 20);
    $("#loader").toggle();
});

$("#main a.doSomethingElse").bind("click", function (e) { 
    e.preventDefault();

    //Other code
});

JSFiddle demo

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

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.