4

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/

2
  • You'll need to post what your myDateObject is. Commented Jun 11, 2012 at 19:28
  • I just added a jsFiddle for an example Commented Jun 11, 2012 at 19:29

3 Answers 3

4

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

$("div").datepicker();
$("div").datepicker('setDate', date);​
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this will probably be what I end up doing but it still doesn't explain why setting the defaultDate option doesn't work
2

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

Apparently the bug doesn't occur with input elements.

1 Comment

So it's a known bug and they consider defaultdate just shouldn't work after initialization DESPITE it being in the API documentation that you can!! WTF
1

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);

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.