I have everything working to toggle between light/dark mode, i just cant seem to figure out how to get the results of the js function to save as a cookie between pages and reloads. I have looked everywhere and just cannot find the proper way to phrase it. I always seem to either have a cookie saved, or the function executes without a cookie. Just cant get both to work. Here is what I have so far. HTML:
<button class="dark-mode" id="theme" onclick="togTheme()">Dark Mode</button>
CSS:
:root {
--navHeight: 70px;
--darkHue: #B0BEC5;
--darkShade: #37474F;
--lightShade: #455A64;
--white: #FFFFFF;
--hue: #ECEFF1;
--shade: #263238;
--black: #222222;
--gray: #eee;
--blue: #0071BC
}
.light-theme {
--themeDropDownBg: var(--goldCrayola);
--themeIconBorderColor: var(--sage);
--navBg: var(--hue);
--navLinkHoverBg: var(--darkHue);
--mainBg: var(--white);
--fontColor: var(--black);
}
.dark-theme {
--themeDropDownBg: var(--msuGreen);
--themeIconBorderColor: var(--richBlackForeground);
--navBg: var(--lightShade);
--navLinkHoverBg: var(--darkShade);
--mainBg: var(--shade);
--fontColor: var(--hue);
}
* {
color: var(--fontColor);
font-family: 'Risque';
transition: background 500ms ease,
padding 500ms linear;
}
body {
margin: 0px;
padding: 0px;
background: var(--mainBg);
}
nav {
background: var(--navBg);
color: var(--fontColor);
}
nav a:hover {
background-color: var(--navLinkHoverBg);
color: var(--blue);
}
nav button {
background: var(--navBg);
color: var(--fontColor);
}
nav button:hover {
background-color: var(--navLinkHoverBg);
color: var(--blue);
}
Javascript:
function togTheme() {
var element = document.body;
element.classList.toggle("dark-theme");
var x = document.getElementById("theme");
if (x.innerHTML === "Dark Mode") {
x.innerHTML = "Light Mode";
} else {
x.innerHTML = "Dark Mode";
}
}
Any help would be greatly appreciated.