3

How do i write a short code that using for(i=1; ... ...); function that allow me to loop through each child and add a different class.

$('#WebPartWPQ3 > table:first-child').addClass('blogpost1');
$('#WebPartWPQ3 > table:nth-child(2)').addClass('blogpost2');
$('#WebPartWPQ3 > table:nth-child(3)').addClass('blogpost3');
$('#WebPartWPQ3 > table:nth-child(4)').addClass('blogpost4');
$('#WebPartWPQ3 > table:nth-child(5)').addClass('blogpost5');
$('#WebPartWPQ3 > table:nth-child(6)').addClass('blogpost6');
$('#WebPartWPQ3 > table:nth-child(7)').addClass('blogpost7');
$('#WebPartWPQ3 > table:nth-child(8)').addClass('blogpost8');
$('#WebPartWPQ3 > table:nth-child(9)').addClass('blogpost9');
0

1 Answer 1

9

try

$('#WebPartWPQ3 > table').each(function(i,j){

 $(this).addClass("blogpost"+(i+1));
});
1
  • 4
    What if #WebPartWPQ3 has child nodes which aren't tables? Might be better to loop over $('#WebPartWPQ3').children() and check if they're a table.
    – DCoder
    May 21, 2012 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.