Jon
Jon

Reputation: 173

jQuery remove all li elements with empty a childs

How do I remove all <li></li> elements with an empty <a></a> tag?

Example:

<li><a href=''>List item</a></li> //Do not delete
<li><a href=''></a></li> //Delete

Upvotes: 4

Views: 158

Answers (3)

PeterKA
PeterKA

Reputation: 24648

To only remove li elements with empty a children use this:

$("li > a:empty").parent().remove();
//OR $('li:has(>a:empty)').remove();

Examples:

<li><a href=''>List item</a></li> <!-- not remove this -->
<li><a href=''></a></li> <!-- remove this -->
<li><span><a href=''></a></span></li> <!-- not remove this -->

Upvotes: 0

Josh Crozier
Josh Crozier

Reputation: 241258

You could combine the :has()/:empty jQuery selectors:

Example Here

$('li:has(a:empty)').remove();

Upvotes: 9

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28475

Use following script

$("li a:empty").parent().remove();

For reference - https://api.jquery.com/empty/

Upvotes: 5

Related Questions