I just wanted to ask whether this is valid as this is the only way my script works as expected:
for (var j = 0; j < items.length; j) {
for (var k = 1; k <= 3; k++) {
$(items.eq(j)).appendTo('.col'+k);
j++;
}
}
notice the j that is not incrementing inside the for statement.
If I increment j, the loop ignores every 4th div for some reason and I can't explain the reason.
Here's a demo of this script.
My main question is whether this is a valid method, and if it's not, do you have a better solution for distributing the divs in each column?
A more natural approach would be iterate items directly:
$(".item").each(function(n) {
$(this).appendTo('.col' + (n % 3 + 1));
});
The modulus operator % makes n cycle over the values 0, 1, 2, 0, 1, 2 etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With