0

I am creating a layout using the 960 grid system and have div items that are displayed automatically. I need to be able to apply an 'alpha' class to the first and 'omega' class to the fourth e.g. div-alpha,div,div,div-omega,div-alpha,div,div,div-omega.

Using the code below it is applying the alpha class to all of the divs :

var n = $("div.item").length;

    $('div .item').filter(function(index) {
    return n % 5 == 1;
        }).addClass('alpha');

        $('div .item').filter(function(index) {
    return n % 5 == 5;
        }).addClass('omega');

How can I achieve this? Many thanks in advance.

areid
  • 283
  • 2
  • 5
  • 14

2 Answers2

0

You could do:

var divs = $("div.item");
var length = divs.length
divs.eq(3).addClass('omega');
divs.eq(0).addClass('alfa');
divs.eq(length-1).addClass('omega');

if there is a container that contains all your divs you could also use the nth-child() selector

Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
0

Try this

var n = $("div.item").length;

    $('div .item').filter(function(index) {
    return index % 5 == 1;
        }).addClass('alpha');

        $('div .item').filter(function(index) {
    return index % 5 == 5;
        }).addClass('omega');
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124