0

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.

3 Answers 3

4

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>
Sign up to request clarification or add additional context in comments.

2 Comments

thanx for the response, but its still not working. One more plugin we are using in the script is draggable, $('.someclass').draggable(); it runs the first time but not after rerendering the partial. and I am using instance variable so i can't move this code into a common js file
@abhas, please check if the html is correctly delivered(not escapted html and js) if you use Rails to generate the code.
2

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.