I have 2 jquery datepickers to select a date range.
Once I have selected a from date (from the first datepicker), I would like to activate the second (to) datepicker.
The code below does that, but for some reason closes the datepicker straight away.
Any ideas? – http://jsfiddle.net/rN4zu/
From <input type="text" id="dateFrom" />
To <input type="text" id="dateTo" />
jQuery
$( "#dateFrom" ).datepicker({
minDate: 0,
maxDate: "+2Y",
showWeek: true,
weekHeader: 'Wk',
onSelect: function( selectedDate ) {
$( "#dateTo" ).datepicker("option", "minDate", selectedDate ).focus();
}
});
$( "#dateTo" ).datepicker({
maxDate: "+2Y",
showWeek: true,
weekHeader: 'Wk'
});
http://jsfiddle.net/8YTKR/
The problem has to do with commands ramping up against each other. The set min date command and
.focusare happening at the same time, which is causing the problem.Ok, so first of all
.focusis not the preferred way of triggering a datepicker. The preferred way is to use the datapicker show method which looks like this.datepicker('show');. So in this example we are going to use that instead.Next we need to make sure that
'show'occurs AFTER the min date command. And because that command has no callback I am going to use setTimeoutWhy 16ms you say? Because its the longest delay possible without it showing up on screen. 16ms is the default jquery animate interval for this very reason.