Search code examples
htmljquerychildren

jQuery: cannot conditionally add class to children elements


I am trying to conditionally add the class "closed-3" to all the children "input" elements of an ancestor. The ancestor should contain the classes ".summon-next-element.on-sub-sub-level". However, I get no effect - no class added, no error in the console, nothing. Here is the jQuery:

$('.summon-next-element.on-sub-sub-level').each(function() {
  if ($(this).find("input").is(":checked")) {
    $(this).nextAll(".summoned-element").first().find("input").addClass("opened-3").removeClass("closed-3");
  } else {
    $(this).nextAll(".summoned-element").first().find("input").addClass("closed-3").removeClass("opened-3");
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="summon-next-element on-sub-sub-level">
  <input type="radio" name="frais_renovation" id="frais_renovation_oui" class="oui">
  <label for="frais_renovation_oui">oui</label>
</div>
<div class="checkbox">
  <input type="radio" name="frais_renovation" id="frais_renovation_non" class="non">
  <label for="frais_renovation_non">non</label>
</div>

<div class="summoned-element">

  <div class="checkbox info-circle right">
    <input type="radio" name="frais_renovation_justificatifs" id="frais_renovation_fichier">
    <label for="frais_renovation_fichier">
                Mes justificatifs 1
            </label>
  </div>

  <div class="checkbox info-circle right">
    <input type="radio" name="frais_renovation_justificatifs" id="frais_renovation_tickets" checked>
    <label for="frais_renovation_tickets">
                Mes justificatifs 2
            </label>
  </div>

</div>
<!-- end of summoned element -->

Thank you so much for your help!


Solution

  • It is also necessary to wrap your logic in the radio button selection event, according to which the reduced assignment / deletion of classes will take place:

    $(this).on('click', function() { ... }
    

    In your code, there was also an incorrect class reference.

    $('div').find('input[type=radio]').each(function() {
      $(this).on('click', function() {
        $('input[type=radio]').addClass("closed-3").removeClass("opened-3");
          if ($(this).is(":checked")) {
            $(this).addClass("opened-3").removeClass("closed-3");
          } 
      });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="summon-next-element on-sub-sub-level">
      <input type="radio" name="frais_renovation" id="frais_renovation_oui" class="oui">
      <label for="frais_renovation_oui">oui</label>
    </div>
    <div class="checkbox">
      <input type="radio" name="frais_renovation" id="frais_renovation_non" class="non">
      <label for="frais_renovation_non">non</label>
    </div>
    
    <div class="summoned-element">
    
      <div class="checkbox info-circle right">
        <input type="radio" name="frais_renovation_justificatifs" id="frais_renovation_fichier">
        <label for="frais_renovation_fichier">
                    Mes justificatifs 1
                </label>
      </div>
    
      <div class="checkbox info-circle right">
        <input type="radio" name="frais_renovation_justificatifs" id="frais_renovation_tickets" checked>
        <label for="frais_renovation_tickets">
                    Mes justificatifs 2
                </label>
      </div>
    
    </div>
    <!-- end of summoned element -->