Is there a way to remove this
<p> </p>
using jQuery?
Try:
$('p')
.filter(function() {
return $.trim($(this).text()) === '' && $(this).children().length == 0
})
.remove()
What that does is it finds all the <p>
s that have nothing in them, and removes them from the DOM.
As Greg mentions above, testing the trimmed .text() will remove paragraphs w/ no text, but do have a self-contained element like the <img>
tag. To avoid, trim the .html() return. As text is considered a child element in the DOM, you'll be set.
$("p").filter( function() {
return $.trim($(this).html()) == '';
}).remove()
This could be a better solution for CMSs. Some rich text editors add
inside empty paragraphs.
$("p").filter( function() {
var html = $(this).html();
if(html == '' || html == ' ')
return true;
}).addClass('emptyP');
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