Rober
Rober

Reputation: 6108

Open jquery datepicker in a specific month

I´m have two jquery datepicket to select a date from and until. When the user selects a from date, I would like the until date would get a default date to from date + 6 days. If I use datepicker setDate to set the until date it works, but it fills the until input field in the form. I don´t want the field is automatically filled out, but just when the user clicks to open the until calendar then the date is selected and the calendar is open in the correct month. I think this probably could be done with defaultDate but it´s not working for me.

See a small example:

var defaultUntil = new Date(since);
defaultUntil.setDate(defaultUntil.getDate() + 6);
$('#until').datepicker("setDate", defaultUntil );

however, this is not working:

var defaultUntil = new Date(since);
defaultUntil.setDate(defaultUntil.getDate() + 6);
$('#until').datepicker("setDefaultDate", defaultUntil );

EDIT: According to @extempl comments I have also tried:

var defaultUntil = new Date(since);
defaultUntil.setDate(defaultUntil.getDate() + 6);
$('#until').datepicker("defaultDate", defaultUntil );

which is not neither working. I think defaultDate only works on init variables. Otherwise, it has to be used setDate. However, this is not what I need because I don´t want the date set in the input field. I have tried defaultDate in the init like: defaultDate: +30, and it works, but I need to apply logic before setDefaultDate, so I have two choices: 1. to be able to set the defaultDate in the logic, not in the init. 2. to be able to do something like defaultDate: function() in the init, and calculate the value inside the function. Is it possible? Not working for me at the moment.

Please note, this is not duplicated with the other post you suggested for this reason.

Upvotes: 0

Views: 1472

Answers (1)

extempl
extempl

Reputation: 3137

To set defaultDate on init you should use

$('#until').datepicker({defaultDate: defaultUntil} )

And to update it later you should use

$('#until').datepicker( "option", "defaultDate", defaultUntil );

Reference

In case updating an option while the datepicker already opened, you should use refresh method.

Upvotes: 2

Related Questions