Fix: Add null check for theme toggle button in script.js

- Prevents errors when theme toggle button is not present on page
- Ensures theme toggle works reliably on all pages that load script.js
This commit is contained in:
Quality App System
2026-01-19 21:33:38 +02:00
parent 04a37501ec
commit 91b798f323

View File

@@ -35,10 +35,12 @@ document.addEventListener('DOMContentLoaded', () => {
// Helper function to update the theme toggle button text
function updateThemeToggleButtonText() {
if (body.classList.contains('dark-mode')) {
themeToggleButton.textContent = 'Change to Light Mode';
} else {
themeToggleButton.textContent = 'Change to Dark Mode';
if (themeToggleButton) {
if (body.classList.contains('dark-mode')) {
themeToggleButton.textContent = 'Change to Light Mode';
} else {
themeToggleButton.textContent = 'Change to Dark Mode';
}
}
}
@@ -52,11 +54,13 @@ document.addEventListener('DOMContentLoaded', () => {
updateThemeToggleButtonText();
// Toggle the theme on button click
themeToggleButton.addEventListener('click', () => {
const isDarkMode = body.classList.toggle('dark-mode');
safeStorage.setItem('theme', isDarkMode ? 'dark' : 'light');
updateThemeToggleButtonText(); // Update the button text after toggling
});
if (themeToggleButton) {
themeToggleButton.addEventListener('click', () => {
const isDarkMode = body.classList.toggle('dark-mode');
safeStorage.setItem('theme', isDarkMode ? 'dark' : 'light');
updateThemeToggleButtonText(); // Update the button text after toggling
});
}
// Date formatting is now handled consistently on the backend