While utilizing the js.cookie.js library available on Github, I encountered a challenge when attempting to set a JSON cookie. According to the documentation for js.cookie.js, in order to set a JSON cookie, the following syntax should be used:
Cookies.set('name', { foo: 'bar' });
Subsequently, if you retrieve the cookie data using Cookies.getJSON('name');
, the output will resemble the following:
{ foo: 'bar' }
Now, my inquiry is how to dynamically set the value of foo
from a JavaScript variable. During my attempt, the approach below did not yield the desired outcome:
var myVar = 'foo';
Cookies.set('name', { myVar: 'bar' });
Ultimately, the result was { myVar: 'bar' }
instead of { foo: 'bar' }
.
To view my test on JSFIDDLE.COM, please visit: https://jsfiddle.net/nz2ur613/