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

108
Vistas
Javascript classList.add to element with 0px height

Not sure if I'm missing something obvious here, but I'm trying to add a class to an element that has 0px height to begin with.

const teamMemberBioShow = function() {
  const bio = document.querySelector(".bio");
  if (bio.classList.contains("hidden")) {
    bio.classList.remove("hidden");
    bio.classList.add("show");
  } else {
    bio.classList.remove("show");
    bio.classList.add("hidden");
  }
}

const teamMemberBioMore = document.querySelector(".bio-more");

teamMemberBioMore.addEventListener('click', teamMemberBioShow);
.bio.hidden {
  opacity: 0;
  height: 0;
}

.bio.show {
  opacity: 1;
  height: auto;
}
<div class="bio hidden">
  Bio goes here
</div>

<div class="bio-more">Read Bio</div>

On click, the class .show is added and the .hidden class is removed. But it doesn't work if the .hidden class has 0px assigned to it.

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

0

The problem is that if you put height 0 but do not set the overflow: hidden the text content of that div will overflow and cover the clickable text, so the click event won't be dispatched:

const teamMemberBioShow = function() {
  const bio = document.querySelector(".bio");
  if (bio.classList.contains("hidden")) {
    bio.classList.remove("hidden");
    bio.classList.add("show");
  } else {
    bio.classList.remove("show");
    bio.classList.add("hidden");
  }
}

const teamMemberBioMore = document.querySelector(".bio-more");

teamMemberBioMore.addEventListener('click', teamMemberBioShow);
.bio.hidden {
  opacity: 0;
  height: 0;
  overflow: hidden;
}

.bio.show {
  opacity: 1;
  height: auto;
}
<div class="bio hidden">
  Bio goes here
</div>

<div class="bio-more">Read Bio</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

This is because even if your element .bio.hidden has an opacity of 0, it messes up with the click event, and it overflows over the other div.

Try putting display: none if you want to hide an element

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