Saving user choice on local storage - need a help

Saving user choice on local storage - need a help
0

#1

I need a help here, I am trying to save user choice in localstorage and reuse it when user visit again, not working as expected, what I am doing wrong here

https://codepen.io/srikanth-kanjarla/pen/dqOzbJ?editors=1011


#2

All values saved to localstorage are strings. You will need to convert your “true” and “false” values back to Boolean values to use your existing if statements or you change your if statement comparisons.

FYI - You can simplify your code to:

const body = document.getElementById('app');
function setBackgroundColor(){
  body.style.backgroundColor = localStorage.darkTheme == "true" ? '#ff0088' : '#eeeeee';
}

body.addEventListener('click', function(e){
  localStorage.darkTheme = e.target.id === 'dark';
  setBackgroundColor();
});

setBackgroundColor();

#3

hey thank you very much