Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop jQuery for each child

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');
like image 241
max li Avatar asked Dec 02 '25 05:12

max li


1 Answers

try

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

 $(this).addClass("blogpost"+(i+1));
});
like image 97
Rafay Avatar answered Dec 03 '25 18:12

Rafay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!