yd0g.sta0203 yd0g.sta0203 - 8 months ago 50
Ruby Question

keep changed style by javascript after showing flash error message in ruby on rails

I have a div part in my HTML(erb), whose display is "none" at first. Then I change its style to "block" detecting the input values of datetimepicker.

I have succeeded to change the style, but the style reverses to "none" if the form gets flash error message shown after validation.

Is there any way to keep the style changed even after error message shows up.

Here is my code of javascript.

$('.datetimepicker').on('dp.change', function(e) {
var x = document.getElementById("from").value;
var y = document.getElementById("to").value;
var date_y = Date.parse(y);
var date_x_day = Date.parse(x) + (1 * 86400000);

if (date_y > date_x_day) {
} else {

I tried to put the line below after "$('#hotel').fadeIn(500); " but it doesn't work.


Could anyone tells me the best way??


So you want to show the hotels after selecting the date, but after the form submission the page will render with a date value and dp.change never get's triggered.

You can extract that anonymous function and call it right after the page renders.

function handleHotelVisibility(){
 // method body;
  $('.datetimepicker').on('dp.change', handleHotelVisibility);