Is there a way to remove this
using jQuery?
$("p").filter( function() {
return $.trim($(this).html()) == '';
}).remove()
I used this to remove empty paragraph that contains not element such as IMG, Input, Select, etc.
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');
give it an id (to get the selector).
<p id="myP"></p>
<script>
$("#myP").remove();
</script>
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()
Probably same answer as here, try to do this on code behind.
With jquery i'll use this:
$("p:empty").remove();
Also you could use .empty(), that will remove all child nodes from the set of matched elements.