Getting function argument from cookie

I have this function to wrap a specific number of divs in a string.

function rowCreation(a) { a = $.cookie("col-layout"); if(typeof(a)==="undefined") a = 3; if ($("div.gallery-item").parent().is("div.row")) { $('div.row').replaceWith(function () { return $('div.gallery-item', this); }); } var divs = $("section#gallery-wrapper div.gallery-item"); for (var i = 0; i < divs.length; i += a) { divs.slice(i, i + a).wrapAll("<div class='row'></div>"); } } 

But something seems wrong, and I can’t understand what exactly. The first line is formed correctly, it contains elements a . But all other divs are placed on one line, even if there are more than a !

0
jquery cookies arguments
Jul 06
source share
1 answer

I think the problem is in the first line:

 a = $.cookie("col-layout"); 

Your rowCreation function treats a as a number, but the cookie passes it as a string. So when you do this,

 for (var i = 0; i < divs.length; i += a) { divs.slice(i, i + a).wrapAll("<div class='row'></div>"); } 

When i = 0,

You are asking for the array to be cut between 1 and "05" (concat i + "a"), this is fine because they exist. Therefore no problem.

When i = 5,

You request that the array be cut between 5 and "15" (concat i + "a"), so it removes all arrays and wraps them in one .row . That is why it is problematic for you.

Try to parse the resulting cookie "col-layout" variable of type Number , for example:

 a = parseInt($.cookie("col-layout"), 10); 

Read more about parseInt here.

+3
Jul 06 '13 at 22:55
source share



All Articles