English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app

Execute Javascript When Using Link_to_function To Include a Partial in Rails


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 w/ #javascript #link_to_function #partial #prototype #replace_htmljavascript #ruby on rails