1319 - BUG - adjusting the js to only run when the targeted DOM element exists

This commit is contained in:
Jonathan Bobel
2024-03-15 10:42:20 -04:00
parent 58a1d8e0c4
commit be2dc9a165
+8 -2
View File
@@ -3,8 +3,13 @@
// Set the target date (10 days before March 15th, 2024) // Set the target date (10 days before March 15th, 2024)
const targetDate = new Date("April 16, 2024 00:00:00").getTime(); const targetDate = new Date("April 16, 2024 00:00:00").getTime();
// Set a const for the container to write a conditional statement and only run if it exists
const countdownContainer = document.getElementById("countdown-container");
// Function to update the countdown display // Function to update the countdown display
function updateCountdown() { function updateCountdown() {
if (countdownContainer) {
const now = new Date().getTime(); const now = new Date().getTime();
const difference = targetDate - now; const difference = targetDate - now;
@@ -14,12 +19,13 @@
// Visibility logic // Visibility logic
if (days < 0 || days > 10) { if (days < 0 || days > 10) {
// Hide if more than 10 days away OR if already past the date // Hide if more than 10 days away OR if already past the date
document.getElementById("countdown-container").style.display = "none"; countdownContainer.style.display = "none";
} else { } else {
// Show if 10 days or less remaining // Show if 10 days or less remaining
document.getElementById("countdown-container").style.display = "block"; countdownContainer.style.display = "block";
document.getElementById("countdown").innerHTML = days + " days "; document.getElementById("countdown").innerHTML = days + " days ";
} }
}
} }