GP24
GP24

Reputation: 877

How to set default date of jQuery datepicker with value already selected

I have two date pickers in a startDate - endDate setup, and need the calendar of the endDate picker to open at the month of the selected startDate.

I have tried setting the default date as below (the date that the calendar opens with))

$("#endDate").datepicker({
        defaultDate: $("#StartDate").datepicker("getDate"))
        });

but from what I have read and tried, it seems it can only be set if the datepicker in question does not have a selected date already, which is always the case on my page, and I guess would often be the case for datepickers generally, so am I missing something obvious?

At the moment I am setting the endDate to the startDate value when it is set, and it works as you might expect, but is not quite right. I don't want to update the endDate, I just want the calendar to open on the start date's month.

This fiddle might help illustrates the issue: http://jsfiddle.net/j7cNk/

Any ideas appreciated.

Upvotes: 0

Views: 934

Answers (1)

adeneo
adeneo

Reputation: 318322

You can use the showCurrentAtPos option to show a certain month

var m1 = $("#StartDate").datepicker("getDate").getMonth();
    m2 = $("#endDate").datepicker("getDate").getMonth();

$('#endDate').datepicker('option', 'showCurrentAtPos', m2-m1 );

Upvotes: 0

Related Questions