mardi 18 octobre 2016

Javasript does not work after j rendering a view

When I dynamically update content to a page using a remote call and a *js.erb file like this:

add_content.js.erb

$("#new_content").html('<%= j render("content") %>');

To render a view like this: _content.html.erb

<div class="name">
    <%= @content.name %>
</div>
<div class="text">
    <%= @content.text %>
</div>

With an javasript file like this: application.js

$(".name").on('click', function(){
  $(".text").slideToggle();
});

... the javascript slideToggle() is not executed. When I do

<%= render("content") %> 

in any view (without js.erb) everything works as expected and slideToggle() works. This is just a quick example to keep it simple, it is not related to slideToggle, even alert("...") etc. will not work.

So my question is why is javascript not executed? Is this related to any loading order?

Aucun commentaire:

Enregistrer un commentaire