0

I'm trying to read an array from a cookie like this:

var arr = $.makeArray($.cookie("mycookie"));

jQuery.each(arr, function() {
  $('#' + this).removeClass('collapsed');
});

The problem is it works only with the first item from the array. Can you help?

1 Answer 1

1

$.makeArray doesn't magically turn strings into arrays. It's for converting array-like objects into proper JavaScript arrays. Example:

> $.makeArray('a b c d')
  ["a b c d"]

...which is probably not what you're looking for.

Your question does not include what the value of $.cookie("mycookie") is, but assuming it's something like 'a b c d', you can just use String.split():

var arr = $.cookie("mycookie").split(' ');

jQuery.each(arr, function() {
  $('#' + this).removeClass('collapsed');
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Now I have a better understanding. The value of the cookie is 'a,b,c,d'
Okay, then just use .split(',') - it sounds like you already figured that out, though.

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.