Martin Petrov
Martin Petrov

Reputation: 2643

jquery cookie array loop

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?

Upvotes: 0

Views: 2228

Answers (1)

Matt Ball
Matt Ball

Reputation: 359816

$.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');
});

Upvotes: 1

Related Questions