0

I am trying to clear a cookie but am having difficulty. Basically, all I am trying to do is change the body background colour based on the value in the cookie. I can create the cookie fine, but my "clearCookie" function doesn't seem to be clearing it like I would expect it to.

function setTheme () {

    if (document.cookie) {
        document.cookie += "; max-age=0";
    }

    document.cookie = $('input:radio[name=theme]:checked').val() + "; max-age=86400; path=/";
    $('body').css('background', document.cookie);
}

function clearCookie () {

    if (document.cookie) {
        document.cookie += "; max-age=0";
        $('body').css('background', '#F2F2F2');
    }

}

$(document).ready(function () {

    if (document.cookie != "") {
        $('body').css('background', document.cookie);
    }

    $('#theme').click(setTheme);
    $('#default').click(clearCookie);

});
braX
  • 11,506
  • 5
  • 20
  • 33
Jordan Carter
  • 1,276
  • 3
  • 19
  • 43
  • Possible duplicate of [javascript - delete cookie](http://stackoverflow.com/questions/2144386/javascript-delete-cookie) – Tigger Apr 07 '16 at 01:51
  • @Tigger I am not sure how to apply this answer to my application, because my cookie does not have a name. – Jordan Carter Apr 07 '16 at 01:53

1 Answers1

1

You should add the path also when clearing:

document.cookie += "; max-age=0; path=/";
trincot
  • 317,000
  • 35
  • 244
  • 286