st4ck0v3rfl0w st4ck0v3rfl0w - 2 years ago 169
HTML Question

How to prevent a click on a '#' link from jumping to top of page?

I'm currently using

tags with jQuery to initiate things like click events, etc.

Example is
<a href="#" class="someclass">Text</a>

But I hate how the '#' makes the page jump to the top of the page. What can I do instead?

Answer Source

In jQuery, when you handle the click event, return false to stop the link from responding the usual way prevent the default action, which is to visit the href attribute, from taking place (per PoweRoy's comment and Erik's answer):

    // Special stuff to do when this link is clicked...

    // Cancel the default action
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download