I have a javascript file called \"front.js\" i load from \"application.js\" like usual with this code:
//= require front
Inside \"front.js\
There are three ways by which you can do it.
a. You can wrap your jquery code inside a function and then call that function on page load. If your function is ready then you can do:
$(document).ready(ready);
$(document).on('page:load', ready);
b. You can use jquery's live event or "on" event:
$(document).on("click","#test",function(){
// your code
});
c. You can use the jquery-turbolinks gem which will bind the Rails Turbolink events to the document.ready events so you can write your jQuery in the usual way. For more information refer to here
It's due to turbolinks, do this instead
var ready;
ready = function() {
$("#test").click(function(event){
...
});
};
$(document).ready(ready);
$(document).on('page:load', ready);
I had the same problem when using foundation in my project, you can disable turoblinks by not requiring turbolinks in your application.js file and it should solve the problem.