href jumps to top of page

While this isn’t a ruby on rails specific issue, I do tend to do this a lot, and never thought twice about it, until it starts bugging the crap out of me. The issue is that you have a link that is used to do some type of javascript, instead of linking. So you add an onclick event, but you need something to link to. I usually put in a # sign:

Do Something

 
<%= link_to 'Do Something', '#', :onclick=>'dosomething()' %>

This is great, except that it jumps you to the top of the page. Very annoying. Here is the solution:

Do Something

 
<%= link_to 'Do Something', '#nogo', :onclick=>'dosomething()' %>

Notice I added “nogo” to my #. This will help prevent from jumping to the top of the page. You can of course use any text you want there.

2 thoughts on “href jumps to top of page

Leave a Reply

Your email address will not be published. Required fields are marked *