I've seen many solutions, but I just don't know how to make them work. This is my code.
const modeButton = document.getElementById('light-dark-btn');
const metroButton = document.getElementById('metro-button');
const body = document.body;
const slider = document.getElementsByClassName('slider');
modeButton.addEventListener('click', ()=> {
let bgColor = document.body.style.backgroundColor
if(bgColor == 'rgb(230, 230, 230)' || !bgColor){
document.body.style.backgroundColor = 'rgb(18,18,18)';
document.body.style.color = 'rgb(230,230,230)';
document.cookie = "darkMode=true";
}else if(bgColor == 'rgb(18, 18, 18)'){
document.body.style.backgroundColor = 'rgb(230,230,230)';
document.body.style.color = 'rgb(0,0,0)';
document.cookie = "darkMode=false";
}
})
body.addEventListener('load', ()=> {
var darkMode = getCookie("darkMode");
if(darkMode == "true"){
body.style.backgroundColor = "rgb(18, 18, 18)";
}
if(darkMode == "false"){
body.style.backgroundColor = "rgb(230, 230, 230)";
}
})
Trying to make the state of a checkbox button stay the same when changing pages.