user2403131
user2403131

Reputation: 43

JQuery - How i can save list in the cookie

I followed this tutorial

http://fricardo.com/manipulando-cookies-com-jquery/

and i saving my list in the cookie:

jQuery.cookie('matrizTela', vList, {expires: 7});

but, if i show in my console.log my cookie:

console.log(jQuery.cookie('matrizTela'));

my return is:

,[object Object] 

Why my return having the "," and dont print my object list ?

PS: my vList is a matrix of the DOM Object

What i need?

I want to save an matrix(a list of DOM Object within the VLIST) and then retrieve the VLIST through this cookie and manipulate the data again.

My Problem?

Having one comma in my return.

Upvotes: 1

Views: 181

Answers (2)

nicosantangelo
nicosantangelo

Reputation: 13716

It appears that the plugin is internally calling .toString() on the object you want to store.

If you want to store it correctly I suggest you do something like

jsonList = JSON.stringify(vList);

jQuery.cookie('matrizTela', jsonList, {expires: 7});

Witch will convert your object in a json string, for example:

JSON.stringify([1,2,3]) // "[1,2,3]"

Then, you could retrieve it like:

jsonList = jQuery.cookie('matrizTela');

console.log( JSON.parse(jsonList) );

Upvotes: 2

user229044
user229044

Reputation: 239291

You can't store complex objects in a cookie. You need to serialize them; try JSON.

Upvotes: 1

Related Questions