Using jQuery's animate(), if the clicked on element is "<a href="#" ...> </a>", the function should still return false?
Asked Answered
R

1

3

I was reading jQuery's page for animate()

http://api.jquery.com/animate/

Its examples don't mention about if using

<a href="#" id="clickme">click me</a>
...

$('#clickme').click(function() {
    $('#someDiv').animate({left: "+=60"});
})

we actually still have to return false like in the old days?

$('#clickme').click(function() {
    $('#someDiv').animate({left: "+=60"});
    return false;
})

(but then, those examples didn't use a <a> for the "click me"... but used something else.

Otherwise the page will jump back to the beginning of the page? Does jQuery have a more elegant or magical way of doing it?

Raft answered 15/6, 2010 at 1:34 Comment(0)
T
3

You need to use event.preventDefault():

$('...').click(function(event) {
    event.preventDefault();
    // Code.
});

From the jQuery Website:

event.preventDefault()
Description: If this method is called, the default action of the event will not be triggered.

Trudey answered 15/6, 2010 at 1:35 Comment(2)
return false works too... is the eventPreventDefault() better?Raft
#2018255Kirkwall

© 2022 - 2024 — McMap. All rights reserved.