[jQuery] Re: Apply class to range of child elements

2009-03-03 Thread ricardobeat
modulus in JS returns the remainder of the division - whatever is left that couldn't be divided returning an integer. 1/4 = 0.25 -> 1%4 = 1 2/4 = 0.5-> 2%4 = 2 3/4 == 0.75 -> 3%4 == 3 4/4 == 1 -> 4%4 == 0 5/4 == 1.25 -> 5%4 == 1 6/4 == 1.5-> 6%4 == 2 and so on... By

[jQuery] Re: Apply class to range of child elements

2009-03-02 Thread Nic Hubbard
Hi Ricardo, Could you explain why you did %4 for the n variable? On Feb 26, 12:16 pm, ricardobeat wrote: > 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' :

[jQuery] Re: Apply class to range of child elements

2009-02-26 Thread mkmanning
So do I :) Using the modulus is best, and without a doubt fastest. On Feb 26, 12:16 pm, ricardobeat wrote: > 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' :

[jQuery] Re: Apply class to range of child elements

2009-02-26 Thread ricardobeat
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){

[jQuery] Re: Apply class to range of child elements

2009-02-26 Thread mkmanning
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 

[jQuery] Re: Apply class to range of child elements

2009-02-26 Thread mkmanning
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){$(l