简体   繁体   中英

Javascript in Assets not working after Rendering Partial in js.erb Ruby on Rails

Question similar to mine are asked before but here the scenario is bit different - I have a js.erb file in which I'm publishing using ( private_pub ) a partial like below

<% publish_to "/conversation/update_waiter" do %>
   $("#online_waiters").replace_html('<%= j(render :partial => "/restaurants/online_waiters")%>');     
<% end %>

the partial is rendered on all users wall successfully and no extra or duplicate div s are generated . But on click function (which is an anchor in newly rendered partial) stops working . While its functionality is defined in /assets/js . Now after showing that partial on users if i refresh the page , then the same partial which is displayed due to my cookies , works fine . Any help or idea will be appreciated

Attach the click handler to a parent element of the generated elements and it will work on all generated elements from partials.

Say you have a div with class content which contains all your html.

$(".content").on("click", "#anchor_one", function(e) {
  console.log("Working!");
});

Or attach it to the document

$(document).on("click", "#anchor_one", function(e) {
  console.log("Working!");
});

Another solution is to add the handler inside your js.erb file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM