xxx12123 xxx12123 - 6 months ago 5x
Javascript Question

style a group of eight elements with nth-of-type?

I want to style a group of divs so that every eight elements get the same left positioning. So not the every eigths element, but a group of eight elements.
I've tried this with css / jquery but it only styles every eighth element, which is wrong.

var cells = $('.board-cells'),
cellUnit = 70,
startPos = -35;

var left;
for(j=0; j<8; j++){
left = startPos+(cellUnit*j+1) + 'px';
for(i=0; i<cells.length; i+=8){
var cellItem = $('.board-cells:nth-of-type('+i+'n+0)');
cellItem.css('left', left);


This should solve your issue:

var cellUnit = 70;
var startPos = -35;

$('.board-cells').each(function (i, el) {
    $(el).css('left', startPos + cellUnit * (i % 8) + 1 + 'px');