I have a function that uses a datepicker. And when choosing which day, I need to get the value of this day and use it in the filterTimes
function.
To do this, I create a variable where there will be a list of all days:
let dayofweek = ['Sun','Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
But how do I now pass the value of the selected day to my function?
I'm trying though to get and test it with:
console.log(dayofweek(e.date.getDay()))
But in the end nothing comes to me in the console.
Here's what my function looks like now:
let restaurantReserve = {
workTime: null,
id: null,
init: function (json, restaurantID) {
let _self = this;
_self.workTime = json;
_self.id = restaurantID;
let dayOfWeek = ['Sun','Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
$('#reservation-date').datepicker({startDate: '+0d'}).on('changeDate', function (e) {
const arDate = e.date.toString().split(' ');
$('#dayofweek').val(arDate[0]);
filterTimes();
let input = $('[name="RestaurantReservationForm[date]"]');
input.val(arDate[3] + '-' + (e.date.getMonth() + 1) + '-' + arDate[2]);
_self.unSetError(input);
$('#reservation-date .js-value').text(arDate[2] + ' ' + arDate[1]);
});
},
getWorkHours: function (json, restaurant_id) {
return json.filter(item => item.restaurant_id == restaurant_id);
},
getWorkHoursForDay: function (json, restaurant_id, day) {
return getWorkHours(json, restaurant_id).filter(item => item.day === day)[0];
},
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");
}
}
}
}
Initially, I had a select on the php page:
<select id="dayofweek" onchange="filterTimes()">
<option value="Mon">Mon</option>
<option value="Tue">Tue</option>
<option value="Wed">Wed</option>
<option value="Thu">Thu</option>
<option value="Fri">Fri</option>
<option value="Sat">Sat</option>
<option value="Sun">Sun</option>
</select>
And I created a variable and took a value from it
let dayOfWeek = document.getElementById("dayofweek").value;