Hello, I haven’t done much coding lately so been reading and started to do a dark theme for my web to keep doing stuff.
I’ve made an icon with: Id = "icon"
And I’ve used jquery to do a toggle class and it works ok, then I’ve been reading about localstorage but I think I’m not understading it right.
This is what i came with:
$(document).ready(function(){
var x = localStorage.getItem("darkTheme");
});
//if (x === 'on') {
//darkmodeOn();
//}
$("#icon").click(function(){
darmodeOn();
if (!$(".container-fluid").hasClass("darktheme")) {
darkmodeOff();
}
});
function darmodeOn(){
$(".container-fluid").toggleClass("darktheme");
$("body").toggleClass("darktheme");
$("#menulogo").attr('src',"imgs/logodark.png");
$("#icon").attr('src',"imgs/sun.png");
localStorage.setItem("darkTheme", "on");
}
function darkmodeOff(){
$("#menulogo").attr('src',"imgs/logo.png");
$("#icon").attr('src',"imgs/moon.png");
localStorage.setItem("darkTheme", "off");
}
I want to be able to store it, so you can navigate form page to page or if you go out and come back later to have your selected theme already set.
The switch works, until I uncomment the commented part where I pretend to read the localstorage and do the switch automatically.