1

I'm using js.cookie.js Github and trying to set a JSON cookie. In documentation of js.cookie.js mentioned that if you want set a JSON cookie you must do like this:

Cookies.set('name', { foo: 'bar' });

so the cookie data will be like below if you call Cookies.getJSON('name');:

{ foo: 'bar' }

Now my question is how to set foo from a javascript variable. I tried this but was not useful:

var myVar = 'foo';
Cookies.set('name', { myVar: 'bar' });

the result was { myVar: 'bar' } and not { foo: 'bar' }

My JSFIDDLE.COM test: https://jsfiddle.net/nz2ur613/

Liam
  • 27,717
  • 28
  • 128
  • 190
ramirez
  • 43
  • 4

1 Answers1

1
document.onload = Do();
   function Do() {
   var myvar = 'foo';
   var obj = {} ;
   obj[myvar] = 'bar';
   Cookies.set('name', obj);
   alert(Cookies.get('name'));
 }

I created an object and then set it to cookie.

Try this one. It will work

Demo : https://jsfiddle.net/snuny9om/

KrishCdbry
  • 1,049
  • 11
  • 19