JQuery onclick jumps to top of page
i had an issue with jquery code where i have a html link doing specific tasks for onclick event. Since the appear on bottom of my page, when i click on the link the browser jumps to top of the page every time i click.
<a href="#" onclick="dofunct()">click me</a>
This is really very frustrating as everytime i click a link, the browser jumps to top of page. After a long search, i fixed the problem by replacing # with javascript void function.
<a href="javascript:void(0);" onclick="dofunc()">click me</a>
It neatly solved my jump problem!
Similar Posts:
- how to reload/refresh a page with jquery?
- How to remove GET variable within $_SERVER['QUERY_STRING'] in PHP
- Fix -> Caret/Cursor jumps to start of textarea in javascript
- Affiliate Hide – Free wordpress plugin to redirect affiliate links!
- Fix -> PHP files are downloading when viewed in Browser
- how to call a php script from javascript?
- Assasins Creed Hangs/Freezing problem in PS3
- Using htaccess to redirect domain without http://
- Fast Website Loading with OpenDNS – Alternative to ISPs DNS Servers
- Problem with Hostgator payment with credit card





August 10, 2009
Thank you! It helps me.
January 26, 2010
Here is another solution:
click me
Returning false prevents the default action of the item you clicked on. I found your solution and this one while trying to solve the same problem. I like this better because you won’t have the ‘javascript:void(0)’ showing up in the bottom corner of your browser when you hover over the link.
February 18, 2010
Thanks, that post has saves me a lot of time debugging!
March 2, 2010
I while back I read that using ‘javascript:void(0);’ was not a best practice.
Instead you can fix this problem by adding ‘return false;’ to your code, which is a more scalable way of fixing this problem.
IE:
$(‘#container’).click(function () {
$(‘#element’).show();
return false;
More info in this other post: http://stackoverflow.com/questions/976753/jquery-click-brings-me-to-top-of-page-but-i-want-to-stay-where-i-am
September 10, 2010
Thank you both – I went with “return false;”, but each option would suffice for my need. Very easy to find solution in Google.
November 9, 2010
Thank you
) It’s good nice.
January 20, 2011
Thanks!!
February 22, 2011
Clear and concise solution, many thanks!
April 12, 2011
You can also do this to solve that problem…
April 20, 2011
Thank you! It helps me.
June 27, 2011
Thank you!
It very helps me.
July 4, 2011
I found that click me
works.
July 4, 2011
I found that click me works
July 4, 2011
I found that <ahref=”#linktobottom”></a>
July 5, 2011
Problem solved, cheers
July 23, 2011
Worked perfectly, thanks a lot!
January 12, 2012
javascript:void(0) worked perfectly in all the cases even though it is not a best pratice. How ever return false will work if you are not returning anything in your function.
In my case my function is returning a value so had to use javascript:void(0).
Thanks for the good trick.
April 13, 2012
You saved my life:D
May 27, 2012
Thank you. Super useful.
September 20, 2012
Thank you it was very simple and helpful
February 7, 2013
wow brilliant nice work i really appreciate this. keep it up.