Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery toggle - unexpected go top

Tags:

jquery

I'm new to jquery, i write a simple snippetto toggle menu . But the problem is : when I scroll at the bot of page and click menu to toggle. It go top unexpectedly .

Thanks for reading .

like image 308
Dzung Nguyen Avatar asked Dec 13 '22 22:12

Dzung Nguyen


2 Answers

You probably have <a href="#">TEXT</a> as your link, right?

href="#" will make the browser scroll up to the top, so add a return false on click, so it looks like this:

<a href="#" onclick="return false">TEXT</a>; alternatively you can return false from your click function to prevent the default behaviour.

like image 85
Jeriko Avatar answered Dec 15 '22 13:12

Jeriko


If the element is an anchor tag, capture the event object, and call the preventDefault() method; as shown below.

$('a#whatever').click(function (event) {
  event.preventDefault();
});
like image 44
Matt Avatar answered Dec 15 '22 14:12

Matt