If i understand your question (and code, because the provided code does more than your specification) you want the following behaviour:
- When a date,
F
, in#from
is selected, you should only be able to select dates greater thanF
in#to
. - When a date,
T
, in#to
is selected, no dates greater than or equal toT
should be selectable in#from
This code will do this:
$(function() {
$('#from').datepicker({
defaultDate: '+5d',
changeMonth: true,
numberOfMonths:1,
minDate:'+0d',
dateFormat: 'DD, MM d, yy',
onClose: function(dateText, inst) {
if (dateText !== '') {
try {
var fromDate = $.datepicker.parseDate(inst.settings.dateFormat, dateText, inst.settings);
fromDate.setDate(fromDate.getDate() + 1);
$('#to').datepicker('option', 'minDate', fromDate);
}
catch (err) {
console.log(err);
}
}
else {
//If #from is empty, restore the original limit in #to
$('#to').datepicker('option', 'minDate', '+0d');
}
}
});
$('#to').datepicker({
defaultDate: '+5d',
changeMonth: true,
numberOfMonths:1,
minDate:'+0d',
dateFormat: 'DD, MM d, yy',
onClose: function(dateText, inst) {
if (dateText !== '') {
try {
var toDate = $.datepicker.parseDate(inst.settings.dateFormat, dateText, inst.settings);
toDate.setDate(toDate.getDate() - 1);
$('#from').datepicker('option', 'maxDate', toDate);
}
catch (err) {
console.log(err);
}
}
else {
//If #to is empty, remove the limit in #from
$('#from').datepicker('option', 'maxDate', null);
}
}
});
});
The two onClose
functions are pretty similar and could be generalized into one function, but I think it's easier to understand what's going on this way.
Also, there's no need for the onClick
handler in #from
or #to
any longer.
Simen Echholt
2010-05-20 21:10:03