I have a checkbox that changes the theme between dark and light, with light being the default.
<div class="switch">
<label>
<input id="layoutSwitch" type="checkbox"><span class="lever"></span>
</label>
</div>
Here is the jQuery code I am using:
$('#layoutSwitch').change(function() {
if($(this).is(":checked")){
$('#bodyLayout').addClass("bodyLayout");
$('#leftMenu').addClass("leftMenu");
}else{
$('#bodyLayout').removeClass("bodyLayout");
$('#leftMenu').removeClass("leftMenu");
}
});
This is my dark theme CSS:
/* Dark Theme */
.bodyLayout {
background-color: #525a69; }
.leftMenu {
background-color: #252d3a; }\
My goal is to keep the selected theme even after reloading the page, as well as maintain the state of the checkbox selection. However, when attempting to combine the following code with the above code, it doesn't work properly and selects all switches on the page instead of just the one selected.
$(function(){
var test = localStorage.input === 'true'? true: false;
$('input').prop('checked', test || false);
});
$('input').on('change', function() {
localStorage.input = $(this).is(':checked');
console.log($(this).is(':checked'));
});