I have a rails app. How can I make .js functions working on elements that were not there on page load but got appended later with AJAX? If I add the code to create.js.erb, then event fires twice. I can't take the code out from the basic document ready page:load function since I need it to work before AJAX call as well.
Asked
Active
Viewed 150 times
1 Answers
0
Your question leads to many other questions. Perhaps you should post some code to provide more of an idea of what your problem is. However, if you want to implement some logic both when the page loads, as well as after an ajax call, then encapsulate your JavaScript in a separate function, and then call that function: 1. On page load; and 2. In the success event of the Ajax call
HTH

measurelink
- 1
- 1
-
Here is another question that is more detailed. I was not sure which way is better to ask. http://stackoverflow.com/questions/34664701/firing-js-in-rails-after-ajax-with-no-turbolinks?noredirect=1#comment57083085_34664701 – Sean Magyar Jan 08 '16 at 03:10
-
measurelink, is it a good way of implementation what you can see in the post I linked? It's working at the moment, but I'm not sure if this is the good approach. I haven't seen this solution before in other sample code. Couldn't it be done somehow with jquery page:change? – Sean Magyar Jan 09 '16 at 23:27