BTB BTB - 18 days ago 5
Javascript Question

Multiple checks in beforeShowDay - jQueryUI datepicker

In short, I want the datepicker to first disable all sundays - I use this code:

jQuery("#datepicker").datepicker({
beforeShowDay: function(day) {
var day = day.getDay();
if (day == 0) {
return [false, "busy"]
} else {
return [true, "free"]
}
}
});


It works. But then I also want to disable specific dates within a range that is stored in an array:

jQuery("#datepicker").datepicker({
beforeShowDay: function (date) {
var dateString = jQuery.datepicker.formatDate('yy-mm-dd', date);
return [dateRange.indexOf(dateString) == -1];
}
});


This also works and disables the days that I want.

Problem: Both codes works seperately - how can I combine them so both sundays are disabled and my custom dates from the array?

Answer

Hope this will help you. Try following code:

$(function(){  
   $('#thedate').datepicker({
      beforeShowDay: function(date) {
         var dateString = jQuery.datepicker.formatDate('yy-mm-dd', date);
         var day = date.getDay();
         if (day == 0 || dateRange.indexOf(dateString) != -1) {
                return [false, "busy"]
         } else {
                return [true, "free"]
         }
     }
  });   
});

Here is working jsfiddle.

Comments