i am facing a problem in javascript event focus and blur i am trying to run a script in a if else condition on a input field text which has a date drop down coming from jqeuryUi i am trying to apply a condition in which if the the user select the today date then i have to run the script otherwise dont run the script
https://camelia.ae/checkout/
here is the link of the website try to add something in the cart
<script>
document.addEventListener("DOMContentLoaded", function() {
var deliveryDateInput = document.getElementById("delivery_date");
deliveryDateInput.addEventListener("focus", function() {
// Remove the previous blur event listener, if any
this.removeEventListener("blur", handleDateBlur);
// Attach the blur event listener
this.addEventListener("blur", handleDateBlur);
});
function handleDateBlur() {
var selectedDateParts = this.value.split("/");
var selectedDate = new Date(selectedDateParts[2], selectedDateParts[1] - 1, selectedDateParts[0]);
var today = new Date();
today.setHours(0, 0, 0, 0);
if (selectedDate.getTime() === today.getTime()) {
alert("Hello! You've selected today's date.");
}
}
});
</script>
i have written this code in which i am trying to show the alert on the today date the problem i am facing is that this code is working but on the second time whe i select first time its shows nothing but when i click second time its show alert
can anyone tell me the solution of this
<script>
document.addEventListener("DOMContentLoaded", function() {
var deliveryDateInput = document.getElementById("delivery_date");
deliveryDateInput.addEventListener("focus", function() {
// Remove the previous blur event listener, if any
this.removeEventListener("blur", handleDateBlur);
// Attach the blur event listener
this.addEventListener("blur", handleDateBlur);
});
function handleDateBlur() {
var selectedDateParts = this.value.split("/");
var selectedDate = new Date(selectedDateParts[2], selectedDateParts[1] - 1, selectedDateParts[0]);
var today = new Date();
today.setHours(0, 0, 0, 0);
if (selectedDate.getTime() === today.getTime()) {
alert("Hello! You've selected today's date.");
}
}
});
</script>
i have written this code in which i am trying to show the alert on the today date the problem i am facing is that this code is working but on the second time whe i select first time its shows nothing but when i click second time its show alert
can anyone tell me the solution of this