Using JQuery Simplifies the whole thing, using JS Cookie
$(document).ready(function(){
// Check cookie and set theme
if(Cookies.get('theme')) {
$('body').removeClass('light-mode dark-mode').addClass( Cookies.get('theme') );
};
//Switch theme and create the cookie...
$("#theme-toggler").click(function(){
if ($('body').hasClass( 'light-mode')){
$('body').removeClass('light-mode').addClass('dark-mode');
Cookies.set('theme', 'dark-mode');
}else {
$('body').removeClass('dark-mode').addClass('light-mode');
Cookies.set('theme', 'light-mode');
}
});
});
Add id
to button
.
<button id="theme-toggler" type="button" name="dark_light" title="Toggle dark/light mode">🌛</button>
Also add this script
<script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>