0

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?

1 Answer 1

3

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");
 });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.