I’m using the jquery date range picker and the example on that page uses ids rather than classes to identify the from and to dates. I needed to convert this to classes and also to make it work on dynamically added elements.
Eventually (one day later) I manage to get it working but the code seems to verbose to me as it is duplicated but I can’t seem to merge it into a simpler routine without breaking the functionality.
Here’s a codepen of the working version:
The part I need help with is this (this bit is working as I want):
//range is ok with this
$(function() {
$('body').on('focus', ".from", function() {
$('.from').datepicker({
defaultDate: "+1w",
changeMonth: true,
dateFormat: 'dd-mm-yy',
numberOfMonths: 2,
onClose: function(selectedDate) {
var theOtherDate = $(this).closest('label').next('label').find('.to');
$(theOtherDate).datepicker("option", "minDate", selectedDate);
// $(this).valid();
}
});
$('.to').datepicker({
defaultDate: "+1w",
changeMonth: true,
dateFormat: 'dd-mm-yy',
numberOfMonths: 2,
onClose: function(selectedDate) {
var theOtherDate = $(this).closest('label').prev('label').find('.from');
$(theOtherDate).datepicker("option", "maxDate", selectedDate);
//$(this).valid();
}
});
})
$('body').on('focus', ".to", function() {
$('.from').datepicker({
defaultDate: "+1w",
changeMonth: true,
dateFormat: 'dd-mm-yy',
numberOfMonths: 2,
onClose: function(selectedDate) {
var theOtherDate = $(this).closest('label').next('label').find('.to');
$(theOtherDate).datepicker("option", "minDate", selectedDate);
}
});
$('.to').datepicker({
defaultDate: "+1w",
changeMonth: true,
dateFormat: 'dd-mm-yy',
numberOfMonths: 2,
onClose: function(selectedDate) {
var theOtherDate = $(this).closest('label').prev('label').find('.from');
$(theOtherDate).datepicker("option", "maxDate", selectedDate);
}
});
})
});
As you can see its basically 2 sections of the same code. I tried reducing it to this:
$(function() {
$('body').on('focus', ".from", ".to", function() {
$('.from').datepicker({
defaultDate: "+1w",
changeMonth: true,
dateFormat: 'dd-mm-yy',
numberOfMonths: 2,
onClose: function(selectedDate) {
var theOtherDate = $(this).closest('label').next('label').find('.to');
$(theOtherDate).datepicker("option", "minDate", selectedDate);
}
});
$('.to').datepicker({
defaultDate: "+1w",
changeMonth: true,
dateFormat: 'dd-mm-yy',
numberOfMonths: 2,
onClose: function(selectedDate) {
var theOtherDate = $(this).closest('label').prev('label').find('.from');
$(theOtherDate).datepicker("option", "maxDate", selectedDate);
}
});
})
});
Now that appears to work but if you select the ‘to’ date first it becomes clear that the range is not working as in the previous example because you don’t get the range limit on the ‘from’ date.
Also in other examples I’ve seen around people are destroying the datepicker before calling it again but this again breaks the function in both versions.
Lastly this needs to work without ids or any unique name attributes.
So to recap the first version (as in the codepen) appears to be working properly but seems overly verbose to me.