Clint C.
Clint C.

Reputation: 688

Remove paragraphs with no text

Is there a way I can remove paragraphs with no text in them, but might have empty html tags in them?

For example

<p><strong><br></strong></p> or <p></p> or <p>&nbsp;</p>

would be removed but

<p><strong>hi<br>there</strong></p> or <p>hi there</p>

wouldn't be removed

Upvotes: 0

Views: 141

Answers (2)

MasterOdin
MasterOdin

Reputation: 7896

Using jQuery to loop through all p elements, then get only the text of it, trim it (so just having spaces also gets removed), and then checking if there's text in it, and if there isn't, remove it.

$('p').each(function() {
    if ($(this).text().trim() == "") {
        $(this).remove();
    }
});

jsfiddle example: http://jsfiddle.net/ygbnpg77/

Upvotes: 2

mattfetz
mattfetz

Reputation: 425

Well if you want to do it front end wise with javascript you could do this.

$(document).ready(function(){
    $('p').each(function(){
           if($(this).html().length == 0)
           {
               $(this).remove();
           }
    })

})

Upvotes: 1

Related Questions