I am trying to use localStorage for the first time to toggle a dark mode status. I am struggling to get it to work when refreshing and changing pages.
Any help with my code in order to understand my errors would be appreciated.
if(!localStorage.getItem('status')) {
localStorage.setItem('status', 'light');
}
dmToggle.onclick = () => {
if (localStorage.getItem('status') === 'light') {
makeDark();
localStorage.setItem('status', 'dark');
} else if (localStorage.getItem('status') === 'dark') {
makeLight();
localStorage.setItem('status', 'light');
}
}
Currently status only toggles after the second click, and page reverts to light mode on refresh or page switch.