Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anchor tag with onclick and href='#' scrolls to top

I have an anchor tag

<a href="#" onclick="Register();" >Register</a>

If you don't have a href in you anchor, it can screw up some stuff, but I don't need to redirect, just call a js function. When this link is clicked however, the href="#" causes it to scroll the page to the top. How do I prevent that from happening?

like image 688
stupidkid Avatar asked Aug 24 '10 02:08

stupidkid


1 Answers

You can add a return false;, like this:

<a href="#" onclick="Register(); return false;">Register</a>

This prevents the default action of the anchor, which is to go to the hash, causing the scroll.

like image 170
Nick Craver Avatar answered Oct 26 '22 22:10

Nick Craver