ruby on rails - Why do coffeescript/jquery functions not trigger on DOM elements loaded via ajax, and how to fix? -


in rails 3.2 app have coffeescript function toggles css class when link clicked.

#coffeescript jquery ->   $(".toggle-link").click ->     $(this).toggleclass "selected"  #view <%= link_to "toggle", my_path, class: "toggle-link" %> 

this works fine.

but if move link ajaxified partial, e.g. pagination, jquery toggle stops working.

why this?

and how can fixed?

you need use on dynamic elements :

jquery ->   $(document).on 'click', ".toggle-link", ->     $(this).toggleclass "selected" 

(replace document container of pages better efficiency)


Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -