-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
33 lines (30 loc) · 1.05 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
document.addEventListener("DOMContentLoaded", () => {
const themeToggleButton = document.getElementById("theme-toggle");
initializeTheme();
themeToggleButton.addEventListener("click", toggleTheme);
});
function initializeTheme() {
const theme = localStorage.getItem("theme") || "light";
const body = document.body;
const themeToggleButton = document.getElementById("theme-toggle");
if (theme === "dark") {
body.classList.add("dark-mode");
themeToggleButton.textContent = "Switch to Light Mode";
} else {
body.classList.remove("dark-mode");
themeToggleButton.textContent = "Switch to Dark Mode";
}
}
function toggleTheme() {
const body = document.body;
const themeToggleButton = document.getElementById("theme-toggle");
if (body.classList.contains("dark-mode")) {
body.classList.remove("dark-mode");
themeToggleButton.textContent = "Switch to Dark Mode";
localStorage.setItem("theme", "light");
} else {
body.classList.add("dark-mode");
themeToggleButton.textContent = "Switch to Light Mode";
localStorage.setItem("theme", "dark");
}
}