jQuery datepicker only initializes once
Hello!
I have a bunch of script part of which involves the jQuery ui datepicker. The datepicker is located on a jQuery modal within a jQuery form. The problem is the datepicker is only firing once, when the modal window is initially opened. If it's closed, the datepicker doesn't initialize again when the window is reopened. ???
I will post this also in the Plugins forum but thought it could be an issue with the overall setup of my script.
Thanks!
Susan
My Code:
- //datepicker
var natDays = [
[1, 1, 'New Year'], //2014
[1, 20, 'Martin Luther King'], //2014
[2, 17, 'Washingtons Birthday'], //2014
[5, 26, 'Memorial Day'], //2014
[7, 4, 'Independence Day'], //2014
[9, 1, 'Labour Day'], //2014
[10, 13, 'Columbus Day'], //2014
[11, 11, 'Veterans Day'], //2014
[11, 27, 'Thanksgiving Day'], //2014
[12, 25, 'Christmas'] //2014
];
// dateMin is the minimum delivery date
var dateMin = new Date();
dateMin.setDate(dateMin.getDate() + (dateMin.getHours() >= 13 ? 1 : 0));
function AddBusinessDays(curdate, weekDaysToAdd) {
var offset = (weekDaysToAdd > 0 ? +1 : -1);
weekDaysToAdd = Math.abs(weekDaysToAdd);
var date = new Date(curdate.getTime());
while (weekDaysToAdd > 0) {
date.setDate(date.getDate() + offset);
//check if current day is business day
if (noWeekendsOrHolidays(date)[0]) {
weekDaysToAdd--;
}
}
return date;
}
function noWeekendsOrHolidays(date) {
var noWeekend = $.datepicker.noWeekends(date);
return (noWeekend[0] ? nationalDays(date) : noWeekend);
}
function nationalDays(date) {
for (i = 0; i < natDays.length; i++) {
if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) {
return [false, natDays[i][2] + '_day'];
}
}
return [true, ''];
}
function setDeliveryDate(date) {
$('#datepicker3').text($.datepicker.formatDate('DD, MM d, yy', AddBusinessDays(date, -2)));
$('#datepicker2').text($.datepicker.formatDate('DD, MM d, yy', AddBusinessDays(date, -3)));
$('#local-delivery-date').text($.datepicker.formatDate('DD, MM d, yy', AddBusinessDays(date, -3)));
}
$('#delivery-date').text($.datepicker.formatDate('DD, MM d, yy', AddBusinessDays(dateMin, 5)));
setDeliveryDate(AddBusinessDays(dateMin, 5));
$.datepicker.setDefaults({
beforeShowDay: noWeekendsOrHolidays,
showOn: 'both',
firstDay: 0,
dateFormat: 'mm/dd/yy',
changeFirstDay: false,
showButtonPanel: true
});
// use datepicker to choose a different delivery date
$('#datepicker').datepicker({
minDate: AddBusinessDays(dateMin, 5),
onSelect: function(datestring) {
setDeliveryDate($(this).datepicker('getDate'));
}
});
$('#LocalDate').datepicker({
minDate: AddBusinessDays(dateMin, 2)
});
//formats dates in modal
var d_names = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var d = new Date();
var curr_day = d.getDay();
var curr_date = d.getDate();
var sup = "";
if (curr_date == 1 || curr_date == 21 || curr_date ==31)
{
sup = "st";
}
else if (curr_date == 2 || curr_date == 22)
{
sup = "nd";
}
else if (curr_date == 3 || curr_date == 23)
{
sup = "rd";
}
else
{
sup = "th";
}
var curr_month = d.getMonth();
var curr_year = d.getFullYear();