Reputation: 173
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
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
Reputation: 241258
You could combine the :has()
/:empty
jQuery selectors:
$('li:has(a:empty)').remove();
Upvotes: 9
Reputation: 28475
Use following script
$("li a:empty").parent().remove();
For reference - https://api.jquery.com/empty/
Upvotes: 5