Search code examples
jqueryruby-on-rails-2link-to-remote

click link on bottom of the page taking to top of the page


I have a link in a page which is a rails link_to_remote. when i click on that link its taking me to top of the page with '#' in url. How can i avoid that?

= link_to_remote "#{stream_thank_yous_count}" ,:url => add_thankyou_stream_path(stream), :method => :get, :before => "event.preventDefault();", :after => "$('ajaxLoaderDiv').hide();", :html => {:class => "icon-smile-o #{get_count_class(stream_thank_yous_count)}"}

HTML generated for this is

<a id="fav_980209445" class="icon-heart fav" onclick="new Ajax.Request('/streams/980209445/mark_as_favourite?social_inbox=false', {asynchronous:true, evalScripts:true, method:'get', parameters:'authenticity_token=' + encodeURIComponent('tHMWDh7R1GZ2t43ejt5aGF4Rds7Jx2nV386JVs5x8wo=')}); $('ajaxLoaderDiv').hide();; return false;" href="#">0</a>

I tried replacing the href with javascript:void(0). using

$j("a.icon-smile-o,a.icon-heart").attr("href", "javascript:void(0)");

But failed to acheive. Can anyone help me how to fix this please.


Solution

  • try this one:

    $("a.icon-smile-o,a.icon-heart").click(function(e){
        e.preventDefault();
    });