maxp maxp - 2 months ago 14
Javascript Question

Setting 'option' on jqueryui DatePicker clears the textbox's value?

I've highlighted the example here:

http://jsfiddle.net/aDxeE/

Basically, a separate event (not known at construction) needs to set

"option","maxDate"
on the datepicker control. This 'clears' the textbox value which is really annoying, as the initial construction does not.

Can anyone offer a solution?

Edit:

Changing line 5 to:

$("#myinput").datepicker("destroy");
$("#myinput").datepicker({minDate:new Date(), maxDate: new Date()});


makes it work as expected, but is a seriously messy approach.

Answer

It's an issue with the format. Your initial value dd-mm-yyyy doesn't match the datepickers default format. To resolve, set the initial value to the correct format and specify the format as dateFormat when creating the datepicker.

Fiddle

Change to:

<input id="myinput" type="text" value="01-01-1970" />

And:

//construct datepicker
$("#myinput").datepicker({minDate:new Date(), dateFormat : "dd-mm-yy"});
//later on, a seperate event changes the maxDate property
//but this 'clears' the existing value in the textbox!
$("#myinput").datepicker("option","maxDate", new Date());
Comments