The problem is the following, there is HTML, for example:
<div class="wrap"> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> </div> It is necessary to add the class "large" to all elements corresponding to the selector :nth(2n+3) :
$('.wrap .item:nth-child(2n+3)').addClass('large'); When the page loads, everything works fine, but there is a functionality that hides some elements in a different order and the class "large" needs to be added only to visible elements and in this case :nth-child() does not work anymore, since the numbering goes on all child elements regardless of whether it is visible or hidden.
You need to add the class "large" to all elements that can be selected using the pseudo :nth-child(8n+3) class :nth-child(8n+3) :nth-child(8n+6) :nth-child(6n+3) :nth-child(6n+4) :nth-child(4n+2) :nth-child(4n+3) , above was just an example.