I’m trying to make a button, to switch the site background once clicked. And of course the setting needs to be saved in a cookie, for it to stick.
How can I combine the two scripts, so that once the button is clicked, it stays switched, with the background? Or is dependend on the body class ‘clicked’?
I found a way to switch and save the background in a cookie:
$(document).ready(function() {
var body_class = $.cookie('body_class');
if(body_class) {
$('body').attr('class', body_class);
}
$("#switch").click(function() {
$("body").toggleClass('clicked');
$.cookie('body_class', $('body').attr('class'));
});
});
And the button is switched from switch_on.png to switch_off.png with this:
$(function(){
$(".toggle-swap").click(function() {
if ($(this).attr("class") == "toggle-swap") {
this.src = this.src.replace("switch_on.png","switch_off.png");
} else {
this.src = this.src.replace("switch_off.png","switch_on.png");
}
$(this).toggleClass("on");
});
});
My button:
<div id="switch"><a href="#"><img class="toggle-swap" src="../img/switch_on.png" alt=""></a></div>
I’d combine that into one function, e.g. like this: