Search code examples
jquerycookiesjquery-cookie

jQuery cookie plugin doesn't read or write cookie


I am trying to use the jQuery Cookie Plugin but it doesn't seem to be reading or writing my data:

var cookieArray = [];
cookieArray['test'] = true;
console.log(cookieArray);

$.cookie('testing', cookieArray, { expires: 10 });
console.log($.cookie('testing') + "hi");

The last log is always just "hi" - Fiddle

I was wondering if there was anything I wasn't including or doing correctly


Solution

  • You have declare differently for object array to store them i.e {} it will make them as Object , but i used to JSON.stringify the array and save them for better manipulation.

    var cookieArray = {};
    cookieArray['test'] = true;
    console.log(cookieArray);
    $.cookie('testing', JSON.stringify(cookieArray), { expires: 10 });
    console.log($.cookie('testing') + "hi");
    

    for these way of doing explained here. fiddle