Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove empty elements from the dom using jQuery?

Tags:

jquery

The WordPress word editor spits out empty <p> tags like this: <p>&nbsp;</p>.

I would like to target all <p> with &nbsp; in them and remove them. I believe I have to use the contains and remove functions but I am not sure if this code would remove the <p> tags with only &nbsp; in them or remove all <p> tags with &nbsp; in them anywhere.

jQuery('p:contains("&nbsp;")').remove();

How would I make this work?

like image 633
John Avatar asked Mar 11 '12 15:03

John


1 Answers

you can use .filter and look if the innerHTML is equal to  :

$("p").filter(function(){
    return $.trim(this.innerHTML) === "&nbsp;"
}).remove();
like image 77
voigtan Avatar answered Sep 27 '22 21:09

voigtan