Written by Sean Behan on Sun Jun 17th 2012

If you use the link_to_function to replace content in a div with content from a partial, any javascript that you include in the partial will not be executed. It is instead included, but will do nothing. Obviously, this isn't the desired behavior. Why would we be taking the trouble to write the javascript in the partial? There is an easy fix. Instead of writing out the script tags, instead use the javascript_tag  method to wrap whatever javascript you would like executed when the partial is loaded.

<%=link_to_function "say hello" {|p| p.replace_html "container", :partial => "say_hello" }%>
And now in my say_hello.erb file
<% javascript_tag do %>
  alert("Hello World");
<% end %>

Tagged with..
#javascript #link_to_function #partial #prototype #replace_html #javascript #Ruby on Rails

Just finishing up brewing up some fresh ground comments...