I'm creating a web project in Ruby on Rails. I have a main.js
file stored inside /app/assets/javascripts/
, in which I've developed a little script to reset the forms.
The script works perfectly just while I don't surf around the web. If I change the current page, the script doesn't work anymore. To get it work again, I have to reload the page through the browser (using Cmd+R or F5).
The main.js
script is supposed to be compiled and it should be available in all the pages in the project, isn't it?
What's wrong?
The code in main.js
:
// Get rid of data in forms
jQuery.fn.reset = function () {
$(this).find("input, textarea, select").each (function() {
if ($(this).is(':checkbox')) {
$(this).prop('checked', false);
}
else {
if (!$(this).is(':hidden') && !$(this).is(':submit')) $(this).val('');
}
});
}
$(document).ready(function() {
// Cancel button in forms
$(".cancel").click(function() {
$(document).reset();
})
});
Application.js:
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require_directory .