Alex Rivera | Logout

Select every nth item in jQuery?

Asked 2009-11-24T17:54:46.810
9

jQuery has the handy :even and :odd selectors for selecting the even- or odd-indexed items in a set, which I'm using to clear every other item in a series of floated boxes, as follows:

<div class='2up'>
   <div> ... </div>
   <div> ... </div>
   ...
   <div> ... </div>
</div>

and

// Clear every 2nd block for 2-up blocks
$('.2up>div:even').css("clear", "both");

This works like a charm.

My question: Is there a straightforward way in jQuery to select every third or fourth item, so I could do the same thing with 3-up or 4-up items?

Edit
Report

1 Answer

1

No, not as such. The filter function will let you do that though.


EDIT:

I stand corrected. Use the n-th child function for simplicity.

answered 2009-11-24T17:59:11.760

Your Answer