user14637541
user14637541

Reputation:

jQuery scripts execution order and optimization

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.

Upvotes: 0

Views: 36

Answers (2)

cloned
cloned

Reputation: 6742

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

Upvotes: 1

kmoser
kmoser

Reputation: 9273

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();

Upvotes: 0

Related Questions