Mark
Mark

Reputation: 488

Remove empty list item with jquery

I have a list which is dynamically built, but there are empty list items which need removing.

<ul>
<li>www</li>
<li>www</li>
<li>www</li>
<li></li>
<li></li>
<li></li>
</ul>

How do I do this with JQuery?

Upvotes: 3

Views: 10232

Answers (3)

jAndy
jAndy

Reputation: 236012

$('ul li:empty').remove();

Upvotes: 18

Jarek
Jarek

Reputation: 5935

$('ul').find('li').each(function(){
    if($(this).is(':empty'))
        $(this).remove();
});

Please use Andy's implementation (above mine :))

Upvotes: 0

Max Shawabkeh
Max Shawabkeh

Reputation: 38603

$('ul li').filter(function() {return $(this).text()​​​​​​​ == '';}).remove();​

Upvotes: 5

Related Questions