Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a parent div if an inner div has a certain class, with javascript

Ok so say I have many divs. Some of the divs, the children have one class, other divs the children have a different class.

I want to hide only the divs which have a child with a certain class.

For example,

<div class="mainDiv">
    <div class="kulkul">
        <div class="childA">
        </div>
    </div>
</div>

<div class="mainDiv">
    <div class="lalala">
        <div class="childB">
        </div>
    </div>
</div>

<div class="mainDiv">
    <div class="kulkul">
        <div class="childA">
        </div>
    </div>
</div>

<div class="mainDiv">
    <div class="lalala">
        <div class="childA">
        </div>
    </div>
</div>

<div class="mainDiv">
    <div class="kulkul">
        <div class="childB">
        </div>
    </div>
</div>

<div class="mainDiv">
    <div class="lalala">
        <div class="childA">
        </div>
    </div>
</div>

Now above, let's say that I only want to hide the parent divs which have a child div with the class .childB

This can't be done with CSS as far as I know (CSS3 anyway), because CSS doesn't allow you to style the parent div, only a child div. And the parent .mainDiv divs (the ones I want to hide) are all exactly the same.

So that leaves javascript.

Using the example above, how can I hide all the .mainDiv divs which contain a child div with the class .childB?

like image 367
agvr3 Avatar asked Jul 12 '16 01:07

agvr3


2 Answers

HIDING PARENT ELEMENT based on its direct descendant

//Update the **sample-element-to-hide** with whatever you wanted to use as a child class with the parent element you wanted to hide e.g., 'childB'
var elementToHideList = document.getElementsByClassName("sample-element-to-hide");
for (var i = elementToHideList.length; i--;)
    elementToHideList[i].parentNode.style.display = "none";

HIDING PARENT ELEMENT based on its child element.

//Solution for the OP
//Update the **childB** with whatever you wanted to use as a child class with the parent element you wanted to hide.
//Note that this would only works if the parent element has a **className** mainDiv. You can change mainDiv with your own parent className.

$('.classB').closest('.mainDiv').hide();
like image 105
JF-Mechs Avatar answered Sep 28 '22 04:09

JF-Mechs


You can do this with pure javascript:

var elementsChildB = document.getElementsByClassName("childB")
for(var i = 0 ; i < elementsChildB.length ; i++){
  elementsChildB[i].parentNode.style.display = "none" ; 
}
<div class="mainDiv">
    <div class="childA">
      a
    </div>
</div>

<div class="mainDiv">
    <div class="childA">
      a
    </div>
</div>

<div class="mainDiv">
    <div class="childB">
      b
    </div>
</div>

<div class="mainDiv">
    <div class="childA">
      a
    </div>
</div>

<div class="mainDiv">
    <div class="childB">
      b
    </div>
</div>

<div class="mainDiv">
    <div class="childA">
      a
    </div>
</div>

Or with Jquery:

$(".childB").parent().hide()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mainDiv">
    <div class="childA">
      a
    </div>
</div>

<div class="mainDiv">
    <div class="childA">
      a
    </div>
</div>

<div class="mainDiv">
    <div class="childB">
      b
    </div>
</div>

<div class="mainDiv">
    <div class="childA">
      a
    </div>
</div>

<div class="mainDiv">
    <div class="childB">
      b
    </div>
</div>

<div class="mainDiv">
    <div class="childA">
      a
    </div>
</div>
like image 40
imazzara Avatar answered Sep 28 '22 04:09

imazzara