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