My writers have a bad habit of creating empty paragraphs. (I.e., paragraphs that have nothing inside them, like:
<p></p>
Is there a jQuery function that removes "empty" paragraphs. Here's what I've tried:
$('p').remove(":contains(' ')"); // nope! bad logic, all p's have empty spaces
$("p:empty").remove() // nope, doesn't work, doesn't remove any p's
Anything else I should try?
$("p:empty").remove()
works for me (demo at http://jsfiddle.net/AndyE/XwABG/1/). Make sure your elements are actually empty and don't contain any white space.
You can always use filter()
:
// Trimming white space
$('p').filter(function () { return $.trim(this.innerHTML) == "" }).remove();
// Without trimming white space
$('p').filter(function () { return this.innerHTML == "" }).remove();
Working demo: http://jsfiddle.net/AndyE/XwABG/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With