I have three different Jquery Ui datepickers I need to put on a single page. The first date picker is a normal "pick-a-day" datepicker, the second will allow users to pick a weekly date range, and the last one will let the user pick a date by month/year.
I can get all three datepickers to work individually but I cannot get them all to work on the same page. As of right all three pickers will only let me pick dates by week when they are all on the same page.
I have tried if/else statements, show hide statements, and about a thousand other things I could think of and nothing seems to work. Any help would be appreciated!
Here is the code for my date pickers:
//datepicker for day
$('#daypicker').datepicker();
//datepicker for week
$(function () {
var startDate,
endDate,
selectCurrentWeek = function () {
window.setTimeout(function () {
$('#weekpicker').datepicker('widget').find('.ui-datepicker-current-day a').addClass('ui-state-active')
}, 1);
};
$('#weekpicker').datepicker({
"showOtherMonths": true,
"selectOtherMonths": true,
"onSelect": function (dateText, inst) {
var date = $(this).datepicker('getDate'),
dateFormat = inst.settings.dateFormat || $.datepicker._defaults.dateFormat;
startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay());
endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6);
$('#weekpicker').val($.datepicker.formatDate(dateFormat, startDate, inst.settings) + ' - ' + $.datepicker.formatDate(dateFormat, endDate, inst.settings));
selectCurrentWeek();
},
"beforeShow": function () {
selectCurrentWeek();
},
"beforeShowDay": function (date) {
var cssClass = '';
if (date >= startDate && date <= endDate) {
cssClass = 'ui-datepicker-current-day';
}
return [true, cssClass];
},
"onChangeMonthYear": function (year, month, inst) {
selectCurrentWeek();
}
}).datepicker('widget').addClass('ui-weekpicker');
$('.ui-weekpicker').on('mousemove', 'tr', function () {
$(this).find('td a').addClass('ui-state-hover');
});
$('.ui-weekpicker').on('mouseleave', 'tr', function () {
$(this).find('td a').removeClass('ui-state-hover');
});
});
//datepicker for month/year
$( "#monthpicker" ).datepicker({
dateFormat: "mm/yy",
changeMonth: true,
changeYear: true
});
And here is the html:
<div class="day">
<label for="daypicker">Month</label>
<input class="datepicker" type="text" id="daypicker">
</div>
<div class="week">
<label for="weekpicker">Month</label>
<input class="datepicker" type="text" id="weekpicker">
</div>
<div class="month">
<label for="monthpicker">Month</label>
<input class="datepicker" type="text" id="monthpicker">
</div>