I am using javascript on my website to switch between normal and dark mode and it works great, the problem is that on page reload it reverts back to normal mode in case the user has chosen dark mode. I would like to know if there is a way to save the mode that the user chooses when reloading the page.
const body = document.querySelector('body'),
sidebar = body.querySelector('nav'),
toggle = body.querySelector(".toggle"),
searchBtn = body.querySelector(".search-box"),
modeSwitch = body.querySelector(".toggle-switch"),
modeText = body.querySelector(".mode-text");
toggle.addEventListener("click" , () =>{
sidebar.classList.toggle("close");
})
searchBtn.addEventListener("click" , () =>{
sidebar.classList.remove("close");
})
modeSwitch.addEventListener("click" , () =>{
body.classList.toggle("dark");
if(body.classList.contains("dark")){
modeText.innerText = "Modo noche";
}else{
modeText.innerText = "Modo día";
}
});