I recently wrote a code to change the mode from light to dark and decided to simplify it to make it more efficient, optimal and shorter but I ran into a problem because I don't want to set the "theme" and "current-theme" attributes. I checked the jQuery docs, just like the logs in the console, I even tried to use data and nothing. If someone would help me simplify this code, I would be grateful or help me solve the problem with unchanging attributes, I would be grateful :)
Full Code:
;(function($){'use strict';
setMode();
$('#theme-mode, #theme-mode i').on('click', function(){
$('body').toggleClass('theme-dark');
});
$('#theme-mode, #theme-mode i').click(function(){
switchMode();
});
function switchMode(){
if($('body').hasClass('theme-dark')){
$('#theme-mode i').removeClass('dd-sun').addClass('fa-moon');
$('body').removeClass('theme-light');
$.cookie("theme_mode", "dark", {expires: 365, secure: true});
}else{
$('#theme-mode i').removeClass('fa-moon').addClass('dd-sun');
$('body').addClass('theme-light').removeClass('theme-dark');
$.cookie("theme_mode", "light", {expires: 365, secure: true});
}
setMode();
}
function setMode(){
if(checkMode('theme_mode')=="dark"){
$('body').addClass('theme-dark');
$('#theme-style').attr('href', 'css/theme-dark.css');
$('#theme-mode i').removeClass('dd-sun').addClass('fa-moon');
}else{
$('body').addClass('theme-light');
$('#theme-style').attr('href', 'css/theme-light.css');
$('#theme-mode i').removeClass('fa-moon').addClass('dd-sun');
}
}
function checkMode(){
var cookieMode = $.cookie("theme_mode");
if(cookieMode != ""){
if(cookieMode=="dark"){
$.cookie("theme_mode", "dark", {expires: 365, secure: true});
}else{
$.cookie("theme_mode", "light", {expires: 365, secure: true});
}
}
return cookieMode;
}
})(jQuery);
Shorted Code:
;(function($){'use strict';
$("#theme-mode").click(function(){
var e=$(this).attr("data-theme"),c=$(this).attr("data-current-theme");
console.log(e+c);
$('body').addClass('theme-'+e);
$("#theme-mode").attr("data-theme",e);
//$("#theme-mode").attr("current-theme",c);
$.cookie("theme_mode",e,{expires:365, secure: true});
$("#theme-style").attr("href", "css/theme/"+e+".css");
if($('body').hasClass('theme-dark')){
$('#theme-mode i').removeClass('dd-sun').addClass('fa-moon');
$('body').removeClass('theme-light');
//$.cookie("theme_mode", "dark", {expires: 365, secure: true});
}else{
$('#theme-mode i').removeClass('fa-moon').addClass('dd-sun');
$('body').addClass('theme-light').removeClass('theme-dark');
//$.cookie("theme_mode", "light", {expires: 365, secure: true});
}
});
})(jQuery);