I have a drop-down menu, where there are items of one type, and items of another type, approximately in this form each item:

<li> Категория <ul> <li class="type1">Пункт тип 1</li> <li class="type2">Пункт тип 2</li> </ul> </li> 

And I put 2 buttons so that each of them hides one of the field types. But there are points in which there is only one type, and it would be desirable that if there are no visible child points at the parent point, then it would also be hidden. How can this be achieved in jquery?

  • Those. if Category has no subcategories, hide it, right? - Kirill Korushkin
  • @KirillKorushkin, not really, if the subcategories are hidden, then hide - zoinx2012

1 answer 1

You can set a parent li to some class and check if they have child uls. If not, hide:

 $('.parent').each(function(){ if($(this).children('ul').size() == 0) { $(this).css('display', 'none'); } }); 
 <li class="parent"> Категория 1 <ul> <li class="type1">Пункт тип 1</li> <li class="type2">Пункт тип 2</li> </ul> </li> <li class="parent"> Категория 2 <ul> <li class="type1">Пункт тип 1</li> <li class="type2">Пункт тип 2</li> </ul> </li> <li class="parent"> Категория 3 </li> <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>