2 Replies - 4177 Views - Last Post: 23 September 2012 - 02:16 PM

#1 kevin_911  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 127
  • Joined: 02-April 09

Jquery Adding and Removing Cookie

Posted 23 September 2012 - 10:47 AM

Hi Guys,

I have written a jquery adding and removing a cookie but I seem to have an issue when removing a specific cookie

Basically this is the code where I insert a cookie inside an array:

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;  	    
});


The above works perfectly, where a value is added as a cookie to my array list like this

74851%2C48877%2C55669

Now in my delete cookie when I try to remove a specific cookie like 48877 from my array
list the whole array list is deleted?

Not sure where I am going wrong in my code?

$(".delcookie a").click(function(e) {
	    var val = $(this).attr("href");
	    if (my_array.indexOf(val) > -1) {
	        $.cookie('Cookies', val, { expires: -1 });
	    }
		e.preventDefault();
		return false;
	});


Please advice?

This post has been edited by kevin_911: 23 September 2012 - 10:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Jquery Adding and Removing Cookie

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4312
  • View blog
  • Posts: 12,090
  • Joined: 18-April 07

Re: Jquery Adding and Removing Cookie

Posted 23 September 2012 - 01:37 PM

Well doing an expires -1 is going to expire the entire cookie which is under the same key "Cookies". Remember that it is a key->value system here so your entire my_array is being placed under the key "Cookies".

Have you tried removing the value from "my_array" and resetting the cookie with the same name? This should overwrite the cookie at the key "Cookies" minus the value since it is no longer in the array.

:)

This post has been edited by Martyr2: 23 September 2012 - 01:38 PM

Was This Post Helpful? 1
  • +
  • -

#3 kevin_911  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 127
  • Joined: 02-April 09

Re: Jquery Adding and Removing Cookie

Posted 23 September 2012 - 02:16 PM

View PostMartyr2, on 23 September 2012 - 02:37 PM, said:

Well doing an expires -1 is going to expire the entire cookie which is under the same key "Cookies". Remember that it is a key->value system here so your entire my_array is being placed under the key "Cookies".

Have you tried removing the value from "my_array" and resetting the cookie with the same name? This should overwrite the cookie at the key "Cookies" minus the value since it is no longer in the array.

:)


Actually I realised that right after I posted this but forgot to update this post.

I tried removing the item from array list but now my jquery is not working :(
It doesn't remove the cookie from the array.
var val = $(this).attr("href");
var index = my_array.indexOf(val) > -1;
if (index = -1) {
       myarray.splice(val);
    }
e.preventDefault();
return false;


Edit: As mentioned above, the cookies are stored like 74851%2C48877%2C55669 do I need to split it and then remove the element?

This post has been edited by kevin_911: 23 September 2012 - 03:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1