I've been struggling with getting a button to properly switch a website to dark mode. I already have a night mode that works based on the user's location and time, so I know the issue lies within my JavaScript...
Initially, I tried adding 'lightMode' first and then removing 'darkMode', but that didn't solve the problem...
<button
type="button"
class="btn btn-outline-secondary"
id="buttonChangeMode"
>
<i class="far fa-moon"></i>
</button>
.lightMode .app {
border: 1px solid #0b0b35;
padding: 20px 15px;
max-width: 1000px;
max-height: 2500px;
margin: 30px auto;
border-radius: 10px;
background-color: white;
}
.darkMode .app {
border: 1px solid #0b0b35;
padding: 20px 15px;
max-width: 1000px;
max-height: 2500px;
margin: 30px auto;
border-radius: 10px;
background-color: rgb(7, 8, 14);
}
function changeMode() {
let mode = document.getElementById("body");
if (mode.classList.contains("lightMode")) {
mode.classList.add("darkMode").remove("lightMode");
} else if (mode.classList.contains("lightMode")) {
mode.classList.remove("darkMode").add("lightMode");
}
}
let buttonChangeMode = document.querySelector("#buttonChangeMode");
buttonChangeMode.addEventListener("click", changeMode);
An error message is showing in the console: "Uncaught TypeError: Cannot read property 'remove' of undefined at HTMLButtonElement.changeMode"