Source : CHECK IF ANY CHECKBOXES ARE SELECTED IN JQUERY
1 2 3 4 5 6 7 8 9 10 11 12 |
function checkChecked(formname) { var anyBoxesChecked = false; $('#' + formname + ' input[type="checkbox"]').each(function() { if ($(this).is(":checked")) { anyBoxesChecked = true; } }); if (anyBoxesChecked == false) { //Do something } } |
Cas un peu plus complexe :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Vérifier qu'au moins une case à cocher est active dans chaque catégorie de filtres // et afficher un témoin en fonction function checkIfAnyBoxIsChecked(){ var anyBoxIsChecked = false; $('ul.Filters-facets__list').each(function(){ if ($(this).find('input[type="checkbox"]').is(':checked')) { anyBoxIsChecked = true; $(this).closest('.facet-nav-item').addClass('has-active-facet'); } }); if (anyBoxIsChecked == false) {} } checkIfAnyBoxIsChecked(); |
Codes HTML et SASS purement indicatifs (ne pas copier/coller en l’état)
1 2 3 4 5 6 7 8 9 |
<li class="facet-nav-item"> <a class="link level0 is-active" href="#"> Marques <i class="active-facet-indicator"></i> </a> <ul class="Filters-facets__list"> <li><input type="checkbox" /></li> <li><input type="checkbox" /></li> </ul> </li> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
.facet-nav{ &-item{ .active-facet-indicator{ display: none; } &.has-active-facet{ .active-facet-indicator{ display: inline-block; content: ''; width: 10px; height: 10px; margin-left: 8px; background-color: $corporate-color-one; border-radius: 50%; } } } } |