Amelie Amelie - 4 months ago 6
jQuery Question

How to select every third element with jquery?

I'm using Bootstrap 3 and styling a card list. The cards have different heights according to their content. I want to add a div.clearfix after every third card.

<div class="col-xs-12 col-sm-6 col-md-4 card-list">
Card here
</div>
<div class="col-xs-12 col-sm-6 col-md-4 card-list">
Card here
</div>


Problem is I can only manage to get the third, but not the sixth, etc.

$('.card-list:nth-child(5)').after('<div class="clearfix"></div>');

Answer

You can select every third item using 3n like $('.card-list:nth-child(3n)')

Comments