Devin Crossman
Devin Crossman

Reputation: 7592

jquery datepicker change default date after initialization

In the jQuery UI datepicker Docs ( http://jqueryui.com/demos/datepicker/ ) it says you can change the default date after init with

$( ".selector" ).datepicker( "option", "defaultDate", +7 );

Where +7 can be a date string, date object, or number of days from today

I can't seem to get this to work. If I set the defaultDate when initializing like this

$(".selector").datepicker({defaultDate:myDateObject});

it works but if I try to use the accessor method I can't get it to work.

Can someone try this and let me know if it works for them and if I've just lost my mind somewhere.

EDIT: Here is a jsFiddle for an example http://jsfiddle.net/Bkw7H/

Upvotes: 4

Views: 10232

Answers (3)

racl101
racl101

Reputation: 4110

I've found that adding options on the load event of the window object works when you want to add options to an already initialized datepicker element.

Like so:

function noSundays(date) {
    var weekday=new Array(7);
    weekday[0]="Sunday";
    weekday[1]="Monday";
    weekday[2]="Tuesday";
    weekday[3]="Wednesday";
    weekday[4]="Thursday";
    weekday[5]="Friday";
    weekday[6]="Saturday";

    if(weekday[date.getDay()] == 'Sunday')
        return [false, '', 'Not open on Sundays'];

    return [true];
}


jQuery(window).load(function(){

    jQuery('.hasDatepicker').datepicker('option', 'maxDate', '+1m +7d');
    jQuery('.hasDatepicker').datepicker('option', 'beforeShowDay', noSundays);

});

Upvotes: 1

j08691
j08691

Reputation: 208002

It's a known bug: http://bugs.jqueryui.com/ticket/6195

Apparently the bug doesn't occur with input elements.

Upvotes: 2

Mothy
Mothy

Reputation: 66

I've created a version that works - you need to use the setdate function, like so.

$("div").datepicker();
$("div").datepicker('setDate', date);​

Upvotes: 4

Related Questions