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

206
Vistas
How do I correct my code so that the 'onclick' toggler component applies only to the icon?

I have a collapsible component made with javascript. I want it to work so that when you click the icon the menu drops down/collapses. The way that it is currently working is that if you click anywhere on the entire line this rule applies.

const collapsibles = document.querySelectorAll(".collapsible");
collapsibles.forEach((item) =>
  item.addEventListener("click", function() {
    this.classList.toggle("collapsible--expanded");
  })
);
.collapsible__header {
  display: flex;
  justify-content: space-between;
}

.collapsible__heading {
  margin-top: 0;
}

.collapsible__chevron {
  transform: rotate(-90deg);
  transition: transform 0.3s;
}

.collapsible__content {
  max-height: 0;
  overflow: hidden;
  opacity: 0;
  transition: all 0.5s;
}

.collapsible--expanded .collapsible__chevron {
  transform: rotate(0);
}

.collapsible--expanded .collapsible__content {
  max-height: 100vh;
  opacity: 1;
}

.collapsible .icon {
  height: 50px;
  width: 50px;
}
<div class="collapsible">
  <header class="collapsible__header">
    <h2 class="collapsible__heading">Item 1</h2>
    <svg class="icon icon--white collapsible__chevron">
      <use xlink:href="images/integratesprite.svg#chevron"></use>
    </svg>
  </header>
  <div class="collapsible__content">
    Lorem ipsum, dolor sit amet consectetur adipisicing elit. Corrupti, adipisci.
  </div>
</div>

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

0

I had to add a span with the class of the SVG since the SVG is invisible.

The code should work on the svg if you remove the span

document.querySelector(".collapsible__chevron").addEventListener("click", function(e) {
  this.closest(".collapsible").classList.toggle("collapsible--expanded");
})
.collapsible__header {
  display: flex;
  justify-content: space-between;
}

.collapsible__heading {
  margin-top: 0;
}

.collapsible__chevron {
  transform: rotate(-90deg);
  transition: transform 0.3s;
}

.collapsible__content {
  max-height: 0;
  overflow: hidden;
  opacity: 0;
  transition: all 0.5s;
}

.collapsible--expanded .collapsible__chevron {
  transform: rotate(0);
}

.collapsible--expanded .collapsible__content {
  max-height: 100vh;
  opacity: 1;
}

.collapsible .icon {
  height: 50px;
  width: 50px;
}
<div class="collapsible">
  <header class="collapsible__header">
    <h2 class="collapsible__heading">Item 1</h2><span class="icon icon--white collapsible__chevron">▼</span>
  </header>
  <div class="collapsible__content">
    Lorem ipsum, dolor sit amet consectetur adipisicing elit. Corrupti, adipisci.
  </div>
</div>

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