Have an account? Sign in
Login  Register  Facebook
jQuery hyperlinks href value onclick
Hi, On my website I use jQuery to hook the events of elements, namely hyperlinks. As these hyperlinks only perform actions on the current page, and do not lead anywhere, I have been putting a href attribute of \"#\" in:
<a href=\"#\">My Link</a>

However in some browsers this causes the page to scroll right to top which is obviously undesirable behaviour. I\'ve tried using a blank href value, or not including one, but then the mouse does not change to the hand cursor upon hovering.

What should I put in there?
Started: September 15, 2011 Latest Activity: November 2, 2011 jquery onclick
2 Answers
or you can do this:
<a onclick="return false;">My Link</a>

Posted: MacOS
In: November 2, 2011

add return false to the end of your click handler, this prevents the browser default handler occurring which attempts to redirect the page:
$(\'a\').click(function() {
// do stuff
return false;
});
Or simply add the return false to the anchor itself:
<a href=\"javascript:return false\">My Link</a>

Bear in mind that the user will see the target of the link in the location bar when hovered over it, so I tend to favour the hash value with a return false in the handler, although this can lead to mistakes when you forget.

Posted: MacOS
In: September 15, 2011

you can use also <a onclick="return false;">My Link</a>
September 15, 2011

that is very good way
September 15, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer