34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
document.addEventListener('DOMContentLoaded', () => {
|
|
const themeToggle = document.getElementById('theme-toggle');
|
|
const body = document.body;
|
|
|
|
// Check for saved theme in localStorage
|
|
const savedTheme = localStorage.getItem('theme');
|
|
if (savedTheme) {
|
|
body.className = savedTheme;
|
|
updateButtonText();
|
|
}
|
|
|
|
// Toggle theme on button click
|
|
if (themeToggle) {
|
|
themeToggle.addEventListener('click', () => {
|
|
if (body.classList.contains('light-mode')) {
|
|
body.className = 'dark-mode';
|
|
localStorage.setItem('theme', 'dark-mode');
|
|
} else {
|
|
body.className = 'light-mode';
|
|
localStorage.setItem('theme', 'light-mode');
|
|
}
|
|
updateButtonText();
|
|
});
|
|
}
|
|
|
|
// Function to update the button text
|
|
function updateButtonText() {
|
|
if (body.classList.contains('light-mode')) {
|
|
themeToggle.textContent = 'Change to dark theme';
|
|
} else {
|
|
themeToggle.textContent = 'Change to light theme';
|
|
}
|
|
}
|
|
}); |