0

I searched through the "If parent element has x, add y css to child" questions but didn't find an answer for my case.

I have a page with some input fields inside multiple div elements. Some of these div elements have a displayNone class, which hides the element. But the parent div of this element has a grey background. So the element self is hidden but I still see the grey background. I want to add the displayNone to the parent div so the grey background will be hidden as well.

Also, adding the displayNone class to the parent div should have no effect on the other div element (with greyBackground class).

I am not able to change the html so I need a jQuery/javascript solution.

I was thinking something like

$('.theClass').each(function() {
    if($("div").hasClass("displayNone")){
      document.getElementById("idElement").style.display = "none";
    }    
});

but i need to add the class to a div element without an id.

Here is some code

CSS

.formcheckbox {
  background: #F2F2F2;; 
}

.greyBackground{
  background: #F2F2F2;; 
}

.displayNone  {
  display: none; 
} 

HTML

<div class="greyBackground">
  <label>Label</label>
  <input type="text"/>
</div><br>

<div class="fieldgrp">
  <div class="formcheckbox">
    <div class="displayNone">
      <div class="field-input ">
          <div class="field">
            <input name="abc" value="10" id="10" type="checkbox">
            <label id="_10" for="10">10 items </label>
          </div>
          <div class="field">
            <input name="abc" value="20" id="20" type="checkbox">
            <label id="_20" for="20">20 items </label>
          </div>
          <div class="field">
            <input name="abc" value="50" id="50" type="checkbox">
            <label id="_50" for="50">50 items </label>
          </div>
      </div>
    </div>
  </div>
</div>

And the FIDDLE

2
  • From description If parent element has x, add y css to child $('.parentEl .childEl').hide() Commented Mar 7, 2017 at 15:03
  • you can traverse through parent element using parentElement property.. this.parentElement.style.display = 'none';" Commented Mar 7, 2017 at 15:03

1 Answer 1

0

I guess your problem is the parent of the element has a padding on it and even if you hide the child elements the element is still visible like this snippet:

.formcheckbox {
  background: purple;
  padding:20px;
}
.displayNone  {
  display: none; 
}
<div class="formcheckbox">
  <label>Label</label>
  <input type="text"/>
</div><br>

<div class="fieldgrp">
  <div class="formcheckbox">
    <div class="displayNone">
      <div class="field-input ">
          <div class="field">
            <input name="abc" value="10" id="10" type="checkbox" class="">
            <label id="_10" for="10" class="">10 items </label>
          </div>
      </div>
    </div>
  </div>
</div>

To solve it the way you want you can use parents() function on Jquery:

$('.displayNone').parents('.formcheckbox').addClass('displayNone')
.formcheckbox {
  background: purple;
  padding:20px;
}
.displayNone  {
  display: none; 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="formcheckbox">
  <label>Label</label>
  <input type="text"/>
</div><br>

<div class="fieldgrp">
  <div class="formcheckbox">
    <div class="displayNone">
      <div class="field-input ">
          <div class="field">
            <input name="abc" value="10" id="10" type="checkbox" class="">
            <label id="_10" for="10" class="">10 items </label>
          </div>
      </div>
    </div>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.