How to remove empty elements after class? (Jquery)

I have a number of elements after a certain class that are empty or have a space inside it.

<div class="post-content">
     <div class="slider-1-smart">
     --- contents of the slider ---
     </div>

     <div></div>
     <div>   </div>

     <ul>
         <li>Text 1</li>
         <li>Text 2</li>
         <li>Text 3</li>
     </ul>  
</div>

This is the code I've tried so far:

$(function() {
    $('.post-content > div:empty').each(function() {

        j(this).remove();

    });
});

The result of this code is that the slider container also disappears. How can I select elements after class = "slider-1-smart" and delete it

Please let me know thanks

+4
source share
2 answers

:emptypsuedo-class selects only those elements that are completely empty. In other words, if it has spaces, it is not technically considered empty.

sibling div .nextAll(), , HTML :

$('.post-content > .slider-1-smart').nextAll('div').filter(function() {
  return $.trim($(this).html()) === '';
}).remove();
+5

, , , . .not("slider-1-smart") .

$(function() {
    $('.post-content > div:empty').not(".slider-1-smart").each(function() {

        $(this).remove();

    });
});
+2

Source: https://habr.com/ru/post/1620654/


All Articles