Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

302
Vistas
If parent div has specific class toggle another div using JS

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 class="hidden-gray">
Display this div only if parent div has class .matched AND child div has class .gray
</div>

<div class="hidden-purple">
Display this div only if parent div has class .matched AND child div has class .purple
</div>

<div class="turn matched" id="1">
    <div class="tile gray">
        <div class="face front" style="background-color: transparent;">
            <img src="https://via.placeholder.com/336x221">
        </div>
    </div>
</div>
      
<div class="turn matched" id="2">
    <div class="tile purple">
        <div class="face front" 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!

JSFiddle here

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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') != '-1' && firstChildx[0].className.indexOf('gray')!= '-1') {
let hideg = document.getElementsByClassName("hidden-gray");
hideg[0].style.display = 'block';
}

if (y.className.indexOf('matched') != '-1' && firstChildy[0].className.indexOf('purple')!= '-1' ) {
let hideg = document.getElementsByClassName("hidden-purple");
hideg[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') != '-1' && firstChildx[0].className.indexOf('gray')!= '-1') {
let hideg = document.getElementsByClassName("hidden-gray");
hideg[0].style.display = 'block';
}

if (y.className.indexOf('matched') != '-1' && firstChildy[0].className.indexOf('purple')!= '-1' ) {
let hideg = document.getElementsByClassName("hidden-purple");
hideg[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 class="hidden-gray">
Display this div only if parent div has class .matched and child div has class .gray
</div>

<div class="hidden-purple">
Display this div only if parent div has class .matched and child div has class .gray
</div>


<div class="turn matched" id="1">
        <div class="tile gray">
            <div class="face front" style="background-color: transparent;">
                <img src="http://via.placeholder.com/336x221">
            </div>
        </div>
    </div>
  
  <div class="turn" id="2">
        <div class="tile purple">
            <div class="face front" style="background-color: transparent;">
                <img src="http://via.placeholder.com/336x221">
            </div>
        </div>
    </div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can try this.

First, add .hidden class to hide your hidden-* div.

.hidden {
  display: none;
}

and add js (check the link).

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda