Ryan
Ryan

Reputation: 1791

jQuery eq() loop

can you help me with this?

$(document).ready(function(){
     $("ul.fam:eq(0) li:eq(2)").addClass("redbold");    
});

In this code, is there a way to loop or increment the '0' value in -> $("ul.fam:eq(0) ? Like making it 0,1,2,3,4,5 and so on... and stop the loop for example when it reaches '3'

Thank you.

Upvotes: 0

Views: 3376

Answers (2)

bozdoz
bozdoz

Reputation: 12870

Loops are sometimes necessary, but buddy Nick Craver probably has the easier answer. Anyway, this is exactly what you asked for.

$(function(){
    for(i=0;i<=2;i++){
     $("ul.fam:eq("+i+") li:eq(2)").addClass("redbold");    
    }
});

Upvotes: 2

Nick Craver
Nick Craver

Reputation: 630549

You can use the :lt() (less than index) selector, like this:

$(document).ready(function(){
  $("ul.fam:lt(4) > li:nth-child(3)").addClass("redbold");    
});

You can test it out here.

This will be the same as selecting :eq(0) through :eq(3). There's also a :gt() selector for the other way around...you can combine both or .slice() to get a range.

Upvotes: 4

Related Questions