Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 605
Jon Avatar asked Dec 09 '22 02:12

Jon


2 Answers

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

Example Here

$('li:has(a:empty)').remove();
like image 115
Josh Crozier Avatar answered Dec 17 '22 16:12

Josh Crozier


Use following script

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

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

like image 35
Nikhil Aggarwal Avatar answered Dec 17 '22 16:12

Nikhil Aggarwal