I am trying to implement a Dark/Light theme switcher in my code. I have got the switcher working but now need a cookie to be set/swapped when the user sets the theme.
Here is my code
$(document).ready(function()
{
$(".light-theme").click(function()
{
$("body").removeClass();
$(this).addClass("current");
$(".dark-theme").removeClass("current");
$(".light-theme a i").removeClass("icon-circle").addClass("icon-circle-ticked");
$(".dark-theme a i").removeClass("icon-circle-ticked").addClass("icon-circle");
$.removeCookie('dark-theme'), { path: '/' };
$.cookie('theme', 'light-theme', { expires: 365, path: '/' });
})
});
$(document).ready(function()
{
$(".dark-theme").click(function()
{
$("body").removeClass().addClass('dark');
$(this).addClass("current");
$(".light-theme").removeClass("current");
$(".dark-theme a i").removeClass("icon-circle").addClass("icon-circle-ticked");
$(".light-theme a i").removeClass("icon-circle-ticked").addClass("icon-circle");
$.removeCookie('light-theme', { path: '/' });
$.cookie('theme', 'dark-theme', { expires: 365, path: '/' });
})
});
$(document).ready(function()
{
if ($.cookie('theme')=='dark-theme') {
$('body').removeClass().addClass('dark');
$(".light-theme").removeClass("current");
$(".dark-theme a i").removeClass("icon-circle").addClass("icon-circle-ticked");
$(".light-theme a i").removeClass("icon-circle-ticked").addClass("icon-circle");
}
if ($.cookie('theme')=='light-theme') {
$('body').removeClass().addClass('light');
$(".dark-theme").removeClass("current");
$(".light-theme a i").removeClass("icon-circle").addClass("icon-circle-ticked");
$(".dark-theme a i").removeClass("icon-circle-ticked").addClass("icon-circle");
}
});
It works fine on desktop but not on mobile. If I select the dark theme, it doesn't stick at all and resets back to the default on refresh.