abhas
abhas

Reputation: 5213

Inline Javascript not working properly on rerendering a partial in ruby on rails 3.1

In my project we have inline javascript in a partial which is using some instance variables. When we are rerendering that partial after successful ajax call that script is not running.

Can you please help me that how I can solve that problem?

For e.g.

_partial.html.erb

 <div class="someclass"></div>
 <script>
    $(document).ready(function(){
       $(".someclass").hide();
    });
 </script>

The scrip tag is not running on replacing this partial from js.erb file.

Upvotes: 0

Views: 2260

Answers (3)

abhas
abhas

Reputation: 5213

Finally, I found the answer which I think were well suited for my requirement is to put the javascript into the js.erb partial and render it accordingly in html.erb and other js.erb files.

Upvotes: 1

Billy Chan
Billy Chan

Reputation: 24815

Your page has already been loaded. The ready event is passed long time ago, nobody is listening that now.

So, if you have $(document).ready in Ajax loaded script, the code will never be executed.

Solution:

Remove "ready" event and fire the code directly:

 <div class="someclass"></div>
 <script>
   $(".someclass").hide();
 </script>

Upvotes: 4

Yauhen Vasileusky
Yauhen Vasileusky

Reputation: 685

It seems to me that ready event is not triggered after manual update of DOM on ajax success event, so callback function is not been executed.

Upvotes: 2

Related Questions