How can I display a div if a parent div has a specific class using JS?
In the code below I would like to display the div with class .hidden-gray only if parent div below has class .matched and child div has class .gray.
And, the div with class .hidden-purple only if parent below div has class .matched and child div has class .purple.
<div >
Display this div only if parent div has class .matched AND child div has class .gray
</div>
<div >
Display this div only if parent div has class .matched AND child div has class .purple
</div>
<div id="1">
<div >
<div style="background-color: transparent;">
<img src="https://via.placeholder.com/336x221">
</div>
</div>
</div>
<div id="2">
<div >
<div style="background-color: transparent;">
<img src="https://via.placeholder.com/336x221">
</div>
</div>
</div>
CSS
.mystyle {
width: 100%;
padding: 25px;
background-color: coral;
color: white;
font-size: 25px;
box-sizing: border-box;
}
.turn {
margin-top: 10px;
}
.hidden-gray {
padding: 20px;
background-color: gray;
color: white;
margin-bottom: 10px;
}
.hidden-purple {
padding: 20px;
background-color: purple;
color: white;
margin-bottom: 10px;
}
Many thanks!
CodePudding user response:
You can try this.
First, add .hidden class to hide your hidden-* div.
.hidden {
display: none;
}
and add js (check the link).
CodePudding user response:
Here's one way of doing it. Here's the JS code I added:
let x = document.getElementById("1");
let y = document.getElementById("2");
let firstChildx = x.children;
let firstChildy = y.children;
if (x.className.indexOf('matched') && firstChildx[0].className.indexOf('gray')) {
let hideg = document.getElementsByClassName("hidden-gray");
hideg[0].style.display = 'block';
}
if (y.className.indexOf('matched') && firstChildy[0].className.indexOf('purple')) {
let hidep = document.getElementsByClassName("hidden-purple");
hidep[0].style.display = 'block';
}
I set .hidden-grayand .hidden-purple to display:none; and then used the js code to check through the class names of the parent div and the first child of the parent div and if it matched the criteria I set the display back to block.
And here it is working:
let x = document.getElementById("1");
let y = document.getElementById("2");
let firstChildx = x.children;
let firstChildy = y.children;
if (x.className.indexOf('matched') && firstChildx[0].className.indexOf('gray')) {
let hideg = document.getElementsByClassName("hidden-gray");
hideg[0].style.display = 'block';
}
if (y.className.indexOf('matched') && firstChildy[0].className.indexOf('purple')) {
let hidep = document.getElementsByClassName("hidden-purple");
hidep[0].style.display = 'block';
}
.mystyle {
width: 100%;
padding: 25px;
background-color: coral;
color: white;
font-size: 25px;
box-sizing: border-box;
}
.turn {
margin-top: 10px;
}
.hidden-gray {
padding: 20px;
background-color: gray;
color: white;
margin-bottom: 10px;
display: none;
}
.hidden-purple {
padding: 20px;
background-color: purple;
color: white;
margin-bottom: 10px;
display: none;
}
<div >
Display this div only if parent div has class .matched and child div has class .gray
</div>
<div >
Display this div only if parent div has class .matched and child div has class .gray
</div>
<div id="1">
<div >
<div style="background-color: transparent;">
<img src="http://via.placeholder.com/336x221">
</div>
</div>
</div>
<div id="2">
<div >
<div style="background-color: transparent;">
<img src="http://via.placeholder.com/336x221">
</div>
</div>
</div>
