I’m trying to remove all empty elements inside a div and found a solution however, it’s using CSS3 selectors and I’m pretty sure it’s not cross browser. Here is what I have working on FF:
$("#container *:empty:not(img)").remove();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is, because jQuery handles that for you, via its Sizzle selector engine; more.
If you want to do it without using
:empty, it’s a fairly straight-forward recursive function (not using jQuery, to avoid creating large arrays in memory):…or something along those lines. If you want to remove text nodes containing just whitespace, then change
to
Live Example | Source