0

In order to cleanup the DOM by some dynamically generated empty divs, I have written the following jQuery scripts, basically I have divs containing empty span and br and div containing nothing, they all need to vanish. By executing them from the console in the following order I have found that they will perfectly clean my document without leaving a single empty div.

$('.release--content div span:empty').remove();

$('.release--content div br:empty').remove();

$('.release--content div:empty').remove();

Is there a way to avoid to run multiple separated scripts by writing a single script? Thank you.

2 Answers 2

1

You can only group the first two selectors into one, since the last one depends on the first two .remove()

$('.release--content div span:empty, .release--content div br:empty').remove();

$('.release--content div:empty').remove(); // the divs are only empty if the first line is run
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it all with a single selector:

$('.release--content div span:empty, .release--content div br:empty, .release--content div:empty').remove();

Or, perhaps easier to read:

$('
  .release--content div span:empty,
  .release--content div br:empty,
  .release--content div:empty
').remove();

2 Comments

I have just tried and the empty divs are not being deleted from the DOM, just the ones containing empty br and span. I need to run the script again to make them vanish.
Try $('.release--content div span:empty,.release--content div br:empty').parent().remove() to remove the parent divs of the empty <span> and <br> tags.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.