Since you've included the tag css then my suggestion is make use of this by using the :nth-child(...)
selector, similar to what Sveen did. .
However I think you need an explanation, I made an example of a simpler way (from my point of view) to be able to modify
var collection = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
var html = '<div>' + collection.join('</div><div>') + '</div>';
document.getElementById('data').innerHTML = html;
#data > div:nth-child(5n),
#data > div:nth-child(5n-1),
#data > div:nth-child(5n-2),
#data > div:nth-child(5n-3),
#data > div:nth-child(5n-4) {
color: red;
}
#data > div:nth-child(10n),
#data > div:nth-child(10n-1),
#data > div:nth-child(10n-2),
#data > div:nth-child(10n-3),
#data > div:nth-child(10n-4) {
color: blue;
}
<div id="data"></div>
Explaining the use of nth-child
First of all I have to say that in this application you will not even have to iterate, you will notice that you do not need for
, join
already solved:
var html = '<div>' + collection.join('</div><div>') + '</div>';
Now about CSS, this first:
#data > div:nth-child(5n),
#data > div:nth-child(5n-1),
#data > div:nth-child(5n-2),
#data > div:nth-child(5n-3),
#data > div:nth-child(5n-4)
Note that:
-
5n
will apply the effect to every 5 elements
-
5n-1
will apply the effect a on the previous element (-1) to the fifth element of each cycle
-
5n-2
will apply the effect a to the previous element in 2 positions (-2) to the fifth element of each cycle
-
5n-3
will apply the effect a to the previous element in 3 positions (-3) to the fifth element of each cycle
-
5n-4
will apply the effect a to the previous element in 4 positions (-4) to the fifth element of each cycle
In this way you will apply to every 5 elements and every 4 elements previous to each of these
Now this:
#data > div:nth-child(10n),
#data > div:nth-child(10n-1),
#data > div:nth-child(10n-2),
#data > div:nth-child(10n-3),
#data > div:nth-child(10n-4) {
color: blue;
}
This is very similar, however the count starts from 10, that is, 10 out of 10 elements, and then comes applying the style to the 4 elements before each of the tenth elements.