tags:

views:

63

answers:

4

I have multiple links that show/hide divs throughout (using slideDown()). Each of the links are

<a href="#">Link</a>

Everytime you click on the link, it jumps back to the top of the screen while it animates. I'm assuming it's because of the #. Is there any way in jQuery to focus on the animation rather than jump to the top of the screen each time? Thanks

+2  A: 

You will need to return false in the function you have bound to the link's click event.

Mark B
That is wrong, he will not have to. He can do so. But if he wants the event to bubble on, he should only call `preventDefault` and not return false from the function.
Tom Bartel
So if what I suggested works, and the question didn't mention event bubbling, how is it wrong?
Mark B
What is event bubbling?
Axsuul
@Axsuul event handlers can cascade... e.g. when you have DOM objects inside each others. If you return false from event handler, the event is considered consumed and the other guys don't get to see it, otherwise it propagates to the parent objects
antti.huima
A: 

Use return false at the end of the click function.

$(function(){
    $("#an1").click(function(){
         // your animation code
         return false;
    });
});

<a id="an1" href="#">click me</a>
rahul
+4  A: 

In the onClick handler, disable the default action of the anchor element ...

$('a').click(function(e) {
e.preventDefault();
// ANIMATION STUFF GOES HERE...
});

beseku
A: 

In the event handler function, use e.preventDefault();, assuming e is the event object. Optionally, return false from the handler function.

Tom Bartel