Search code examples
javascriptjqueryhtmldom-traversal

Input check affecting all classes


How can I add a class using jquery without it affecting all divs assigned to that class name? There has to be an easy way around this? I'm already using $(this) for the input check, so how can I make the action unique to the toggle it's on?

$('input:checkbox').change(function(){
   if($(this).prop('checked')==true){ 
      $(this).closest(".row").toggleClass('highlight', this.checked); 
      $('.overlay').fadeIn(300).delay(3000).fadeOut();
      $(".loader").addClass('active').fadeIn(300).delay(3000).fadeOut();
      setTimeout(function(){
         $(".complete").addClass('active').delay(3300).fadeIn(300).delay(1000).fadeOut()
      }, 3100);
   } else {
      $('.loader').hide(); 
      $('.complete').hide(); 
      $('.overlay').hide(); 
      $(".error").addClass('active').fadeIn(300).delay(2000).fadeOut(200)
   }
});

My jsfiddle.


Solution

  • Just like you are using $(this).closest(".row"), instead of $('.loader'), $('.complete') and $('.error') you can use:

    var container = $(this).closest(".toggle-container")
    container.find('.loader')
    container.find('.complete')
    container.find('.error')
    

    Demo below:

    $('input:checkbox').change(function() {
      var container = $(this).closest(".toggle-container");
      if ($(this).prop('checked')) {
        $(this).closest(".row").toggleClass('highlight', this.checked);
        $('.overlay').fadeIn(300).delay(3000).fadeOut();
        container.find(".loader").addClass('active').fadeIn(300).delay(3000).fadeOut();
        setTimeout(function() {
          container.find(".complete").addClass('active').delay(3300).fadeIn(300).delay(1000).fadeOut()
        }, 3100);
      } else {
        container.find('.loader').hide();
        container.find('.complete').hide();
        $('.overlay').hide();
        container.find(".error").addClass('active').fadeIn(300).delay(2000).fadeOut(200)
      }
    });
    html,
    body {
      height: 100%;
      width: 100%;
    }
    
    .container {
      width: 100%;
      background: #edf1f4;
    }
    
    .overlay {
      background: rgba(128, 128, 128, 0.5);
      display: none;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
    }
    
    .row {
      display: flex;
      background: #fcfcfc;
    }
    
    .highlight {
      z-index: 700;
      background: none;
      position: absolute;
    }
    
    .title {
      font-size: 16px;
      font-weight: 400;
      line-height: 24px;
    }
    
    
    /* toggle item  */
    
    .toggle-item {
      text-align: right;
    }
    
    .toggle {
      position: relative;
      display: inline-block;
      vertical-align: middle;
      width: 60px;
      height: 34px;
    }
    
    .toggle input {
      display: none;
    }
    
    .slider {
      position: absolute;
      cursor: pointer;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #ccc;
      -webkit-transition: .4s;
      transition: .4s;
      text-align: initial;
    }
    
    .slider:before {
      position: absolute;
      content: "";
      height: 26px;
      width: 26px;
      left: 4px;
      bottom: 4px;
      background-color: white;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    input:checked+.slider {
      background-color: #0272B6;
    }
    
    input:focus+.slider {
      box-shadow: 0 0 1px #0272B6;
    }
    
    input:checked+.slider:before {
      -webkit-transform: translateX(26px);
      -ms-transform: translateX(26px);
      transform: translateX(26px);
    }
    
    .toggle-label {
      font-size: 9px;
      line-height: 11px;
      letter-spacing: 0.56px;
      font-weight: 600;
      color: #fff;
      text-transform: uppercase;
      display: inline-block;
    }
    
    .toggle-label:first-child {
      margin: 10px 8px 0 10px;
    }
    
    
    /* rounded sliders */
    
    .slider.round {
      border-radius: 34px;
    }
    
    .slider.round:before {
      border-radius: 50%;
    }
    
    .toggle-container {
      display: inline-block;
    }
    
    .toggle-container:first-child {
      margin-right: 30px;
    }
    
    .toggle-title {
      font-weight: 400;
      display: inline-block;
      vertical-align: middle;
    }
    
    
    /* end of toggle item */
    
    .loader {
      border: 2px solid #ccc;
      border-top: 2px solid #0272B6;
      border-radius: 50%;
      width: 24px;
      height: 24px;
      animation: spin 2s linear infinite;
      vertical-align: middle;
      margin: 0 5px 0 0;
      display: none;
      position: relative;
    }
    
    .complete,
    .error {
      width: 20px;
      height: 20px;
      vertical-align: middle;
      position: relative;
      display: none;
    }
    
    .inactive {
      display: none;
    }
    
    .active {
      display: inline-block;
    }
    
    @keyframes spin {
      0% {
        transform: rotate(0deg);
      }
      100% {
        transform: rotate(360deg);
      }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="overlay"></div>
    <div class="row">
      <!-- toggle 1 -->
      <div class="toggle-container">
        <div class="loader"></div>
        <div class="complete"><img src="https://s3.amazonaws.com/my-account-prototype/green-check.png" alt="complete"></div>
        <div class="error"><img src="https://s3.amazonaws.com/my-account-prototype/red-warning.png" alt="error"></div>
        <p class="toggle-title">Email</p>
        <label class="toggle" for="check-1">
            <input type="checkbox"id="check-1" class="checkmark">
            <div class="slider round data-usage">
             <span class="toggle-label">on</span><span class="toggle-label">off</span>
             </div>
         </label>
      </div>
      <!-- toggle 2 -->
      <div class="toggle-container">
        <div class="loader"></div>
        <div class="complete"><img src="https://s3.amazonaws.com/my-account-prototype/green-check.png" alt="complete"></div>
        <div class="error"><img src="https://s3.amazonaws.com/my-account-prototype/red-warning.png" alt="error"></div>
        <p class="toggle-title">Text</p>
        <label class="toggle" for="check-2">
            <input type="checkbox"id="check-2" class="checkmark">
            <div class="slider round data-usage">
              <span class="toggle-label">on</span><span class="toggle-label">off</span>  
            </div>
          </label>
      </div>
    </div>