I have multiple pages like Home, Faq and more and I've added darkmode function and I want website to remember the choice. If I have pressed darkmode on home and refresh the choice is saved. However when I go to Faq the value is reset even tho I've pasted the same code on my other website. Yes I've tried connecting same JS file to these 2 websites and didn't worked.
btw it's shortened code as example so don't mind that
button.addEventListener('click', () => {
if (element.style.color == "wheat") {
element.style.color = "rgb(33,33,36)";
window.localStorage.setItem("mode", "White");
} else {
element.style.color = "wheat"
window.localStorage.setItem("mode", "Dark");
}
});
var ls = localStorage.getItem("mode");
if (ls == "Dark") {
console.log("it's dark");
} else {
console.log("it's white");
}
So yeah if I click the button and refresh website it will be "it's dark" but if I click and go to Faq with same connected js file it will be "it's white". Sorry if it's a stupid question I'm still learning and I'm not sure if it should work with multiple pages maybe I should use different method like cookies? Thanks for any help.