Is there a way to create and recall a site preference cookie to remember a user's dark or light theme?
I am using a "Turn the Lights Off" button on the navigation bar to toggle a users dark or light theme settings and would like to set a cookie to remember:
-
[](#)
Here is the jQuery toggle:
// DARK & LIGHT BULB SITE SETTINGS
// GET DEFAULT THEME FROM ADAPTIVE-COLOR-SCHEME
var toggle = $('#theme-setting');
if ($('body').hasClass('dark')) {
toggle.addClass('bg-dark text-white dark-theme').attr('title','Change to Light Theme');
$('').appendTo(toggle);
} else {
toggle.addClass('bg-light text-dark light-theme').attr('title','Change to Dark Theme');
$('').appendTo(toggle);
}
// OVERRIDE ADAPTIVE-COLOR-SCHEME ON CLICK
toggle.click(function(e) {
e.preventDefault();
if ($(this).hasClass('light-theme')) {
$(this).removeClass('bg-light text-dark light-theme').addClass('bg-dark text-white dark-theme').attr('title','Change to Light Theme').html('');
$('body').addClass('dark');
} else {
$(this).removeClass('bg-dark text-white dark-theme').addClass('bg-light text-dark light-theme').attr('title','Change to Dark Theme').html('');
$('body').removeClass('dark');
}
});
How can I store and recall a user's theme choice and override "adaptive-color-scheme" using a cookie?
