v14nt0
v14nt0

Reputation: 255

Change option dynamically in JQuery UI DatePicker fails

I want to make date range selection using jquery-ui datepicker. First change at #dteStart succeed to set minDate at #dteEnd. But #dteEnd failed to refresh its options on next change, if i alert DateOptions.minDate its value changed according to dateMin.

Maybe i miss something here...

$(document).ready(function () 
{
    $("#dteStart").datepicker()
    .change(function () 
    {
        dateStart = $(this).datepicker('getDate');
        dateMin = new Date(dateStart.getTime());
        dateMin.setDate(dateMin.getDate() + 1);

        var DateOptions = {
            dateformat: "mm/dd/yyyy",
            minDate: dateMin
        }
        $("#dteEnd").datepicker(DateOptions);
    });
});

TIA,

REV

Upvotes: 15

Views: 30043

Answers (4)

Malik
Malik

Reputation: 3

$('#divbspDate').data("DateTimePicker").destroy();

You can try this, for Date time picker it is working.

Upvotes: 0

Torge
Torge

Reputation: 2284

If you just want to change the already configured options, you can also do:

$("#dteEnd").datepicker("option", DateOptions);

or

$("#dteEnd").datepicker("option", { dateFormat: "mm/dd/yyyy" });

Upvotes: 26

bencergazda
bencergazda

Reputation: 670

The following jQuery helper function may be useful in such cases to preserve the original options:

$.fn.customizeDatepicker = function(newOptions) {
    var prevOptions = $(this).datepicker('option', 'all');
    $(this).datepicker('destroy').datepicker($.extend(prevOptions, newOptions));
    return this;
};

It saves the previous options and extends them with the new options.

Upvotes: 5

TheVillageIdiot
TheVillageIdiot

Reputation: 40527

put $("#dteEnd").datepicker("destroy"); before $("#dteEnd").datepicker(DateOptions); and it will work fine.

Upvotes: 32

Related Questions