user1031743
user1031743

Reputation: 329

Remove and update value from cookie js.cookie script

I use this script for manipulating cookies: https://github.com/js-cookie/js-cookie/tree/v2.0.3#readme

What would be the right syntax to update existing cookie? For example I would like to read values (comma separated strings), remove first string and update the same cookie. Should I remove cookie and set it back with new values or is there a better how to do it using this script? Thank you.

Upvotes: 1

Views: 461

Answers (2)

BenG
BenG

Reputation: 15154

Try this. Turn the result into an array. Get the first item with shift and set the cookie again.

Cookies.set('test', 'foo,bar,foo2,bar2');

var cookie = Cookies.get('test').split(',');
var first = cookie.shift();

Cookies.set('test', cookie.join(","));

Fiddle

Upvotes: 1

mwl
mwl

Reputation: 1488

Don't remove cookie, just set it again:

Cookies.set('foo', 'aaa,bbb,ccc');
var value = Cookies.get('foo');
Cookies.set('foo', value.substr(value.indexOf(',') + 1));

Upvotes: 1

Related Questions