Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
132
rated 0 times [  138] [ 6]  / answers: 1 / hits: 19773  / 12 Years ago, mon, may 21, 2012, 12:00:00

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');

More From » jquery

 Answers
77

try



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

$(this).addClass(blogpost+(i+1));
});

[#85464] Friday, May 18, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckaylab

Total Points: 311
Total Questions: 120
Total Answers: 93

Location: Montenegro
Member since Thu, Jun 16, 2022
2 Years ago
;