views:

53

answers:

1

Hi all - I know how to gray out the weekends on the jQuery Datepicker by instantiating it like this:

$('.calendar').datepicker({ beforeShowDay: $.datepicker.noWeekends });

The Datepicker also automatically selects the current date, so if the current date happens to be on a weekend, nothing is selected.

How can I make it select the next weekday if the currentdate falls on a weekend?

Thanks a lot!

Al

A: 

You can loop through and find the next valid date based on whatever beforeShowDay function you're using and set the defaultDate property to the result, like this:

var sd = new Date();
for(var i = 0; i < 7; i++) {
    if($.datepicker.noWeekends(sd)[0]) break; //found a valid day, hop out
    sd.setDate(sd.getDate() + 1); //move to the next day
}
$('.calendar').datepicker({ 
    beforeShowDay: $.datepicker.noWeekends,
    defaultDate: sd
});

Here's a demo that works for today (otherwise you could only see it work on weekends :)

Nick Craver
works great - thanks a lot!!
Al