JQuery removes all li elements with empty child

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

Example:

<li><a href=''>List item</a></li> //Do not delete
<li><a href=''></a></li> //Delete
+4
source share
3 answers

You can combine :has()/ :emptyjQuery selectors:

Example here

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

Use the following script

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

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

+5
source

To remove only items liwith 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 -->
0
source

Source: https://habr.com/ru/post/1598744/


All Articles