yd0g.sta0203 yd0g.sta0203 - 2 months ago 14
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) {
$('#hotel').fadeIn(500);
} else {
$('#hotel').fadeOut(500);
}
});


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

document.getElementById('hotel').style.display="block";


Could anyone tells me the best way??

Answer

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;
}
$(function(){
  handleHotelVisibility();
  $('.datetimepicker').on('dp.change', handleHotelVisibility);
});
Comments