Hide grand-parent class when checkbox is checked

Viewed 59

I need to hide the first class below (wfob_wrap) when checkbox (wfob_checkbox) is checked. I tried many ways with no success.

Could anyone help me with this?

Thank you!

<div id="wfob_wrap" class="wfob_wrap_start">
  <div class="wfob_wrapper" data-wfob-id="72396">
    <div class="wfob_bump wfob_clear" data-product-key="wfob_5fb3c4fc409e4" data-wfob-id="72396">
      <div class="wfob_outer">
        <div class="wfob_Box">
          <div class="wfob_bgBox_table no_table">
            <div class="wfob_bgBox_tablecell no_table_cell wfob_check_container">
              <div class="wfob_order_wrap wfob_content_bottom_wrap">
                <div class="wfob_bgBox_table_box">
                  <div class="wfob_bgBox_cell wfob_img_box">
                    <div class="wfob_checkbox_input_wrap">
                      <span class="wfob_bump_checkbox">
                        <input type="checkbox" class="wfob_checkbox wfob_bump_product" checked="">
                      </span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
3 Answers

You can use something like this:

document.querySelector('[type=checkbox]').addEventListener('change', e => {
  e.target.closest('#wfob_wrap').firstElementChild.style.display = 'none';
});
<div id="wfob_wrap" class="wfob_wrap_start">
  <div class="wfob_wrapper" data-wfob-id="72396">
    <div class="wfob_bump wfob_clear" data-product-key="wfob_5fb3c4fc409e4" data-wfob-id="72396">
      <div class="wfob_outer">
        <div class="wfob_Box">
          <div class="wfob_bgBox_table no_table">
            <div class="wfob_bgBox_tablecell no_table_cell wfob_check_container">
              <div class="wfob_order_wrap wfob_content_bottom_wrap">
                <div class="wfob_bgBox_table_box">
                  <div class="wfob_bgBox_cell wfob_img_box">
                    <div class="wfob_checkbox_input_wrap">
                      <span class="wfob_bump_checkbox">
                        <input type="checkbox" class="wfob_checkbox wfob_bump_product" checked="">
                      </span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

I've used .closest(..) to get the #wfob_wrap element and .firstElementChild to get this element's first child.

In this specific case, since the element you're looking for has an id, you don't need .closest(..), you can get that element directly using document.getElementById('wfob_wrap').

I gave two solutions for javascript and jquery. Was it necessary?

jquery version:

$('.wfob_checkbox.wfob_bump_product').click(function(){
  if($(this).is(':checked')) {
     $(this).closest('#wfob_wrap .wfob_wrapper').css('display', 'none');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="wfob_wrap" class="wfob_wrap_start">
  <div class="wfob_wrapper" data-wfob-id="72396">
    <div class="wfob_bump wfob_clear" data-product-key="wfob_5fb3c4fc409e4" data-wfob-id="72396">
      <div class="wfob_outer">
        <div class="wfob_Box">
          <div class="wfob_bgBox_table no_table">
            <div class="wfob_bgBox_tablecell no_table_cell wfob_check_container">
              <div class="wfob_order_wrap wfob_content_bottom_wrap">
                <div class="wfob_bgBox_table_box">
                  <div class="wfob_bgBox_cell wfob_img_box">
                    <div class="wfob_checkbox_input_wrap">
                      <span class="wfob_bump_checkbox">
                        <input type="checkbox" class="wfob_checkbox wfob_bump_product">
                      </span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

javascript version:

let checkbox = document.querySelector('.wfob_checkbox.wfob_bump_product');

checkbox.onclick = function() {
  if (this.checked) {
    this.closest('.wfob_wrapper').style.display = 'none';
  }
}
<div id="wfob_wrap" class="wfob_wrap_start">
  <div class="wfob_wrapper" data-wfob-id="72396">
    <div class="wfob_bump wfob_clear" data-product-key="wfob_5fb3c4fc409e4" data-wfob-id="72396">
      <div class="wfob_outer">
        <div class="wfob_Box">
          <div class="wfob_bgBox_table no_table">
            <div class="wfob_bgBox_tablecell no_table_cell wfob_check_container">
              <div class="wfob_order_wrap wfob_content_bottom_wrap">
                <div class="wfob_bgBox_table_box">
                  <div class="wfob_bgBox_cell wfob_img_box">
                    <div class="wfob_checkbox_input_wrap">
                      <span class="wfob_bump_checkbox">
                        <input type="checkbox" class="wfob_checkbox wfob_bump_product">
                      </span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Try this

It can handle that the checkbox is checked on load

The first is for one checkbox, then second for many.

const toggle = function() {
  const chk = document.querySelector(".wfob_checkbox");
  chk.closest(".wfob_wrapper").classList.toggle("hide", chk.checked)
};  
window.addEventListener("load",function() {
  document.querySelector(".wfob_checkbox").addEventListener("click",toggle)
  toggle()
});
.hide { display:none; }
<div id="wfob_wrap" class="wfob_wrap_start">
  <div class="wfob_wrapper" data-wfob-id="72396">
    <div class="wfob_bump wfob_clear" data-product-key="wfob_5fb3c4fc409e4" data-wfob-id="72396">
      <div class="wfob_outer">
        <div class="wfob_Box">
          <div class="wfob_bgBox_table no_table">
            <div class="wfob_bgBox_tablecell no_table_cell wfob_check_container">
              <div class="wfob_order_wrap wfob_content_bottom_wrap">
                <div class="wfob_bgBox_table_box">
                  <div class="wfob_bgBox_cell wfob_img_box">
                    <div class="wfob_checkbox_input_wrap">
                      <span class="wfob_bump_checkbox">
                        <input type="checkbox" class="wfob_checkbox wfob_bump_product">
                      </span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

If you have more sets, you cannot use IDs

window.addEventListener("load", function() {
  document.getElementById("container").addEventListener("click", function(e) {
    const tgt = e.target;
    if (tgt.classList.contains("wfob_checkbox")) {
      tgt.closest(".wfob_wrap_start").querySelector(".wfob_wrapper").classList.toggle("hide", tgt.checked)
    }
  });
  
  // loading
  [...document.querySelectorAll(".wfob_checkbox:checked")].forEach(tgt => 
      tgt.closest(".wfob_wrap_start").querySelector(".wfob_wrapper").classList.toggle("hide", tgt.checked)
  )    
  
});
.hide {
  display: none;
}
<div id="container">

  <div class="wfob_wrap_start">
    <div class="wfob_wrapper" data-wfob-id="72396">
      <div class="wfob_bump wfob_clear" data-product-key="wfob_5fb3c4fc409e4" data-wfob-id="72396">
        <div class="wfob_outer">
          <div class="wfob_Box">
            <div class="wfob_bgBox_table no_table">
              <div class="wfob_bgBox_tablecell no_table_cell wfob_check_container">
                <div class="wfob_order_wrap wfob_content_bottom_wrap">
                  <div class="wfob_bgBox_table_box">
                    <div class="wfob_bgBox_cell wfob_img_box">
                      <div class="wfob_checkbox_input_wrap">
                        <span class="wfob_bump_checkbox">
                        <input type="checkbox" class="wfob_checkbox wfob_bump_product">
                      </span>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="wfob_wrap_start">
    <div class="wfob_wrapper" data-wfob-id="72396">
      <div class="wfob_bump wfob_clear" data-product-key="wfob_5fb3c4fc409e4" data-wfob-id="72396">
        <div class="wfob_outer">
          <div class="wfob_Box">
            <div class="wfob_bgBox_table no_table">
              <div class="wfob_bgBox_tablecell no_table_cell wfob_check_container">
                <div class="wfob_order_wrap wfob_content_bottom_wrap">
                  <div class="wfob_bgBox_table_box">
                    <div class="wfob_bgBox_cell wfob_img_box">
                      <div class="wfob_checkbox_input_wrap">
                        <span class="wfob_bump_checkbox">
                        <input type="checkbox" class="wfob_checkbox wfob_bump_product" checked>
                      </span>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

</div>

Related