I am not able to figure out how to delete a cookie inside a array?
Basically I have a array where I store all my cookies. Now I am trying to
delete a ‘cookie’ with a click event but not able to figure out why the cookie list
doesn’t get updated?
Perhaps I need to reset the cookie list?
Please advice.
Here is my code for inserting a cookie:
var my_array = [];
$(".addCookie a").click(function(e) {
var val = $(this).attr("href");
if (my_array.indexOf(val) == -1) { my_array.push(val); }
$.cookie('Cookies', my_array);
e.preventDefault();
return false;
});
and this for deleting a cookie present inside a array
$(".remCookie a").click(function(e) {
var val = $(this).attr("href");
var index = my_array.indexOf(val);
if (index == -1) {
my_array.splice(index, 1);
}
e.preventDefault();
return false;
});
There is two problems when you remove a cookie; you splice the array if the index is equal to -1 (i.e. it the value isn’t in the array), and you don’t put the array into the cookie.
});