Question

Have the following if statement checking for the presence of a cookie and whether it's true or false. For some reason it's not working.

Here's the code:

// the if statement

  if($.cookie('style-widget-state') === true){
    $('.style-widget').addClass('style-toggle-show');
  };

// setting the cookie on a toggleClass method

  $('.style-tab').on('click', function() {
    $(".style-widget").toggleClass("style-toggle-show");
    $.cookie('style-widget-state', $('.style-widget').hasClass('style-toggle-show'), {expires:365, path: '/'});
    $(this).toggleClass("ai-cogs ai-arrow-left5");
    return false;
  });

Much obliged for any insights.

Was it helpful?

Solution

in cookies the value is stored as a string, so your comparison using === will fail, try

if($.cookie('style-widget-state') === 'true'){
}

Demo: Fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top