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
Post a Comment