color switching is working i am so happy

This commit is contained in:
zoe 2022-04-09 23:45:40 +02:00
parent 873317081b
commit ff99214248
4 changed files with 67 additions and 36 deletions

View file

@ -1,29 +1,32 @@
const checkbox = document.getElementById('darkmode-toggle');
const body = document.querySelector("body");
const checkbox = document.getElementById("darkmode-toggle");
const colorswitchers = document.getElementsByClassName("colorswitch");
if (localStorage.getItem('dark')) {
switchToDark()
if (localStorage.getItem("dark")) {
switchToDark();
} else {
switchToLight()
switchToLight();
}
checkbox.checked = localStorage.getItem('dark')
checkbox.checked = localStorage.getItem("dark");
checkbox.addEventListener("click", function () {
localStorage.setItem("dark", this.checked)
if (this.checked) {
switchToDark()
} else {
switchToLight()
}
})
localStorage.setItem("dark", this.checked);
if (this.checked) {
switchToDark();
} else {
localStorage.removeItem("dark");
switchToLight();
}
});
function switchToLight() {
// for some reason this needs to be set to null
// i am losing my mind
localStorage.removeItem("dark")
body.classList.add('light')
for (let item of colorswitchers) {
item.classList.add("light");
}
}
function switchToDark() {
body.classList.remove('light')
for (let item of colorswitchers) {
console.log(item);
item.classList.remove("light");
}
}