I am trying to create and store a cookie with the jQuery Cookie Plugin. I want to cookie to just be an ordinary counter. I need it to be a cookie because I want to keep the counter going if the page is refreshed. When some condition is true I want, 1 to be added to the cookie value. It seems simple but I am having trouble with using the plugin.
I have created the cookie like this:
$(document).ready(function(){
$.cookie("cookieValue", "0", { expires: 7 , path: '/' });
});
A small example of what I’m trying to achieve:
if (/*some condition*/) {
cokieValue++;
}
That did not work when the condition was true, the cookie value remained at 0. I also tried:
$(document).ready(function(){
$.cookie("cookieValue", "0", { expires: 7 , path: '/' });
var cookieValue = parseInt($.cookie("cookieValue"));
if (/*some condition*/) {
cookieValue++;
}
});
This also did not work – cookieValue remained at 0. Any suggestions on how I can accomplish this?
You need to check to see if it exists before you update it with zero.
You need to save the value after you update it.
So the final code would look like