I have a datepicker in which I create a variable and determine which day of the week we select
let dayofweek1 = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
let dayOfWeek = dayofweek1[e.date.getDay()];
console.log(dayOfWeek)
Full function:
$('#reservation-date').datepicker({startDate: ' 0d'}).on('changeDate', function (e) {
const arDate = e.date.toString().split(' ');
let input = $('[name="RestaurantReservationForm[date]"]');
let dayofweek1 = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
let dayOfWeek = dayofweek1[e.date.getDay()];
console.log(dayOfWeek)
input.val(arDate[3] '-' (e.date.getMonth() 1) '-' arDate[2]);
_self.unSetError(input);
$('#reservation-date .js-value').text(arDate[2] ' ' arDate[1]);
});
And I want to use this dayOfWeek variable in another function
filterTimes: function () {
if ((["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"].indexOf(dayOfWeek) >= 0)) {
let workHours = getWorkHoursForDay(json, restaurantID, dayOfWeek);
let items = document.querySelectorAll(".dropdown-menu.dropdown-menu-height-fixed li a");
for (let item of items) {
let itemValue = parseFloat(item.getAttribute('data-value'));
item.parentNode.classList[((itemValue < parseFloat(workHours.open)) || (itemValue > parseFloat(workHours.close)) ? "add" : "remove")]("invisible");
}
}
}
How can I declare this variable with the value of the selected day globally?
CodePudding user response:
- Try declaring the variable outside the function.
- I've read that the variables that are not declared are scoped globally, but this is not a best practice.