if ($(this).hasClass('ui-closed')) { $(".markdown-block .sqs-block-content h4").nextUntil("h4").slideUp(); $(".markdown-block .sqs-block-content h4").removeClass('ui-open'); $(".markdown-block .sqs-block-content h4").addClass('ui-closed'); } $(this).nextUntil("h4").slideToggle(); $(this).toggleClass('ui-closed ui-open'); }); });