Alex Rivera | Logout

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

Asked 2010-07-15T05:36:30.827
277

I'm currently using <a> tags with jQuery to initiate things like click events, etc.

An example of what I'm doing is: <a href="#" class="someclass">Text</a>.

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

Edit
Report

1 Answer

280

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):

$('a.someclass').click(function(e)
{
    // Special stuff to do when this link is clicked...

    // Cancel the default action
    e.preventDefault();
});
answered 2010-07-15T05:37:57.367

Your Answer