简体   繁体   中英

Add class to parent div if it has a span as child

I have number of filters to show products in my shop. I'm using bootstrap-select to style the filter (dropdowns).

When I change a filter value, the other filters automatically hide options if there are no results available for that combination.

The problem is, that there a re now Filters that show that don't have any available options to choose from. In this case I want to hide the Filter completely.

If there are options available the code looks like this:

 <div class="form-group "> <label>Serie</label> <div class="btn-group bootstrap-select show-tick js-wpv-filter-trigger form-control open"><button type="button" class="btn dropdown-toggle bs-placeholder btn-dropdown btn-default" data-toggle="dropdown" role="button" data-id="wpv_control_select_wpcf-serie" title="Bitte wählen..." aria-expanded="true"> <span class="filter-option pull-left">Bitte wählen...</span> <span class="bs-caret"> <span class="caret"></span> </span> </button> <div class="dropdown-menu open" role="combobox"> <ul class="dropdown-menu inner" role="listbox" aria-expanded="true"> <li data-original-index="0"><a tabindex="0" class="" data-tokens="null" role="option" aria-disabled="false" aria-selected="false"><span class="text">D-9</span><span class="fontawesome fa fa-check check-mark"></span></a></li> </ul> </div> <label class="custom-select"> <select id="wpv_control_select_wpcf-serie" name="wpv-wpcf-serie[]" class="selectpicker js-wpv-filter-trigger form-control" multiple="multiple" tabindex="-98"> <option value="D-9">D-9</option> </select> <span></span> </label> </div> </div> 

If there is no option available, the code looks like this:

 <div class="form-group"> <label>Passend für Helm</label> <div class="btn-group bootstrap-select show-tick js-wpv-filter-trigger form-control open"> <button type="button" class="btn dropdown-toggle bs-placeholder btn-dropdown btn-default" data-toggle="dropdown" role="button" data-id="wpv_control_select_wpcf-kompatibilitat-extern-mechanisch" title="Bitte wählen..." aria-expanded="true"> <span class="filter-option pull-left">Bitte wählen...</span> <span class="bs-caret"> <span class="caret"></span> </span> </button> <div class="dropdown-menu open" role="combobox"> <ul class="dropdown-menu inner" role="listbox" aria-expanded="true"></ul> </div> <label class="custom-select"> <select id="wpv_control_select_wpcf-kompatibilitat-extern-mechanisch" name="wpv-wpcf-kompatibilitat-extern-mechanisch[]" class="selectpicker js-wpv-filter-trigger form-control" multiple="multiple" tabindex="-98"> </select> <span></span> </label> </div> </div> 

My goal is to add a class .hide-filter to .form-group if the child .dropdown-menu has no li

try below code where you can iterate each form-group and find if it contains dropdown with no li.

 $(function(){ $('.form-group').each(function(){ var $dropdown = $(this).find('.dropdown-menu'); if($dropdown.find('li').length==0) { $(this).addClass('hide-filter'); } }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div class="form-group"> <label>Passend für Helm</label> <div class="btn-group bootstrap-select show-tick js-wpv-filter-trigger form-control open"><button type="button" class="btn dropdown-toggle bs-placeholder btn-dropdown btn-default" data-toggle="dropdown" role="button" data-id="wpv_control_select_wpcf-kompatibilitat-extern-mechanisch" title="Bitte wählen..." aria-expanded="true"><span class="filter-option pull-left">Bitte wählen...</span>&nbsp;<span class="bs-caret"><span class="caret"></span></span></button> <div class="dropdown-menu open" role="combobox"> <ul class="dropdown-menu inner" role="listbox" aria-expanded="true"></ul> </div><label class="custom-select"><select id="wpv_control_select_wpcf-kompatibilitat-extern-mechanisch" name="wpv-wpcf-kompatibilitat-extern-mechanisch[]" class="selectpicker js-wpv-filter-trigger form-control" multiple="multiple" tabindex="-98"></select><span></span></label></div> </div> 

This is another approach:

$( document ).ready(function() {
    if($( ".dropdown-menu.inner:has(li)" ).length == 0){
      $( ".form-group" ).addClass( "hide-filter" );
    }
});  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM