Reputation: 5025
I've searched for tutorials on how to set cookies with jQuery (using the jQuery cookie plugin) and they all seem to be oriented towards people more experienced than me.
Here's some example code:
<button>Example</button>
<div id="whatever" style="background:red;">Test</div>
<script>
$('button').click(function() {
$('#whatever').css("background","yellow");
});
</script>
How can I keep #whatever
's background yellow via cookie?
Upvotes: 0
Views: 180
Reputation: 11588
Well, it's easy peasy:
//on document ready, checks if the cookie is set, and if so, sets the background with it's value
$(function(){
if($.cookie("background") != null){
$('#whatever').css("background", $.cookie("background"));
}
});
//here you set the cookie, with the value you want
$('button').click(function() {
$('#whatever').css("background","yellow");
$.cookie("background", "yellow");
});
Upvotes: 3