3

I am currently trying to loop through an array and append id's to my current jquery cookie but cant see to get it working the way I'd like to.

$.cookie('sampleCookie', 'true');
$.cookie.json = true;
var arrayTest=[{id: 'id1', key: 'true'},{id: 'id2', key: 'true'},{id: 'id3', key: 'true'}];

                    for (var i = arrayTest.length - 1; i >= 0; i--) {

                        var currentId = arrayTest[i].id;
                        $.cookie('sampleCookie' + '&' + currentId);             
                    }

This code block doesnt seem to produce the output I am looking for.

I'd like the cookie to look like "true&id1&id2&id3"

1
  • 1
    It's usually $.cookie(name, value), it seems you're trying to store the value in a very complicated name instead ? Commented Nov 14, 2013 at 23:10

1 Answer 1

4

Thanks Adeneo,

this worked for me.

$.cookie('sampleCookie', $.cookie('sampleCookie') + '&' + currentId);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi. I have tried your code which is perfect for me but I am getting first undefined value in my cookie. Any Idea about this. This is my result: undefined, Chrysanthemum.jpg, Desert.jpg. I have also check by alerting but I am not getting undefined anything.
This is my jQuery cookie code: jQuery.cookie('dropzone_added', jQuery.cookie('dropzone_added') + ', ' + a.name);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.