Buonasera,
ho provato svariate volte ad adattare un codice della localStorage da cui stavo prendendo spunto ai temi in mio possesso. Ahimè nervoso eliminai quello che avevo fatto. Chiedo un aiuto da parte vostra per favore in modo che riusciate ad adattarlo. Il codice da cui stavo prendendo spunto in allegato.
codice:
var themeSwitch = document.getElementById('themeSwitch');if(themeSwitch) {
initTheme(); // on page load, if user has already selected a specific theme -> apply it
themeSwitch.addEventListener('change', function(event){
resetTheme(); // update color theme
});
function initTheme() {
var darkThemeSelected = (localStorage.getItem('themeSwitch') !== null && localStorage.getItem('themeSwitch') === 'dark');
// update checkbox
themeSwitch.checked = darkThemeSelected;
// update body data-theme attribute
darkThemeSelected ? document.body.setAttribute('data-theme', 'dark') : document.body.removeAttribute('data-theme');
};
function resetTheme() {
if(themeSwitch.checked) { // dark theme has been selected
document.body.setAttribute('data-theme', 'dark');
localStorage.setItem('themeSwitch', 'dark'); // save theme selection
} else {
document.body.removeAttribute('data-theme');
localStorage.removeItem('themeSwitch'); // reset theme selection
}
};
}
So di chiedere troppo, ma faccio fatica a comprendere quello che avevo sbagliato.
Grazie in anticipo della risposta
NewTechSlyDev_