<div class="product__description--long"> <h5></h5> <p></p> <p></p> <p></p> <p></p> <h5></h5> <p></p> <p></p> <p></p> <h5></h5> <p></p> </div> <script> $(document).ready(function(){ var desTitles = $('.product__description--long h5'); for(var i= 1; i<=desTitles.length; i++) { var wrapSelector = $('.product__description--long h5:nth-of-type('+i+')').nextUntil('.product__description--long h5:nth-of-type('+(i+1)+')'); wrapSelector.wrapAll("<div class='accordion-description__text'></div>") var checkTitle = $('.product__description--long h5:nth-of-type('+i+')'); checkTitle.addClass('accordion-description__title') if(checkTitle.text() == '') { checkTitle.css('display','none'); } } $('.product__description--long h5').click(function(){ $(this).toggleClass('active'); $(this).next().toggleClass('active'); }); }); </script>