I like it cleaner:

$('a').each(function(i){
   var n = Math.floor(i/10) % 4;
   $(this).addClass(
      n == 0 ? 'first' :
      n == 1 ? 'second' :
      n == 2 ? 'third' :
      n == 3 ? 'fourth' : '');
});

or

var classNames = ['first', 'second', 'third', 'fourth'];
$('a').each(function(i){
   var n = Math.floor(i/10) % 4;
   $(this).addClass(classNames[n]);
});

This also gives you support for infinite expansion. An index of 45
will evaluate to class 'first', 51 to 'second', 85 to 'first' again
and so on.

cheers,
- ricardo

On Feb 26, 6:29 am, mkmanning <michaell...@gmail.com> wrote:
> And just as an exercise, here's the 'jQuery' way chained:
>
> $('a').filter(':lt(10)').addClass('first').end().filter(':gt(9):lt
> (10)').addClass('second').end().filter(':gt(19):lt(10)').addClass
> ('third').end().filter(':gt(29)').addClass('fourth');
>
> Not really any speed gain though.
>
> On Feb 26, 1:21 am, mkmanning <michaell...@gmail.com> wrote:
>
> > It's possible. Here's a more traditional way:
>
> >         $('a').each(function(i,link){
> >             if(i<10){$(link).addClass('first');}
> >             else if (i>9 && i<20){$(link).addClass('second');}
> >             else if (i>19&&i<30){$(link).addClass('third');}
> >                 else if (i>29&&i<40){$(link).addClass('fourth');}
> >         })
>
> > Here's a more 'jQuery' way:
> >         $('a:lt(10)').addClass('first');
> >         $('a:gt(9):lt(10)').addClass('second');
> >         $('a:gt(19):lt(10)').addClass('third');
> >         $('a:gt(29):lt(10)').addClass('fourth');
>
> > Which is better? The first takes a little over half as long as the
> > second.
>
> > On Feb 25, 10:45 pm, Nic Hubbard <nnhubb...@gmail.com> wrote:
>
> > > I have a list of links, around 40 of them.  I want to apply classes to
> > > groups of them.  So, items 1-10 I want to apply a class to, then
> > > 11-20, then 21-30 and 31-40, each of these groups should have their
> > > own class.
>
> > > Is something like this possible?  I looked through the jQuery
> > > selectors and could not find a solution.

Reply via email to