0

I just recently started trying to use the cookie library js-cookie:

Found here: https://github.com/js-cookie/js-cookie

I can't seem to determine whether my cookies are being set or read correctly. The main goal is to read if a checkbox is checked, create a cookie and use that on another page to change some elements.

Here's how I attempted setting the cookie:

$('#food').click(function() {
   if ($(this).is(':checked')){
      Cookies.set('food', 'true');
   }
   else{
      Cookies.remove('food');
   }
});

It properly reads the checkbox being checked on click, so I know it is at least reaching the cookie set.

For reading the cookie value:

if(Cookies.get('food') === 'true'){
   $('#food').css("color", "#198a80");
}

I followed the documentation on the repository page for setting and it looks correct. I assumed reading the cookie just passed the value portion as a normal variable and it could be compared like normal, but the code just does nothing, so I guessed the problem was either with the setting or reading of the cookie.

Photon
  • 9
  • 5
  • Developer Tools -> Storage -> Cookies – Andreas Mar 04 '20 at 08:58
  • [How can I debug my JavaScript code?](https://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) – Andreas Mar 04 '20 at 09:00
  • Thanks. I did find the cookie section in the dev tools. Now I know that the cookies aren't actually being set to begin with. Unfortunately, I'm not the most familiar with the js-cookie library at the moment, so that's as far as I can get in diagnosing the issue with why the code isn't working. I followed their example code pretty close so I'm out of ideas. – Photon Mar 04 '20 at 09:58

0 Answers0