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

211
Vistas
How to make independent toggle buttons?

So that everyone opens their own content. Making an individual identifier is a bad idea, there are 1000 such buttons

  <button onclick="showHide()" class="toggle__button">click</button>
  <h1 class="toggle__elements hide">1</h1>


  <button onclick="showHide()" class="toggle__button">click</button>
  <h1 class="toggle__elements hide">2</h1>


  <button onclick="showHide()" class="toggle__button">click</button>
  <h1 class="toggle__elements hide">3</h1>


<style>
  .hide{
    display:none;
  }
</style>
let userSection = document.querySelector(".toggle__elements");
let isShow = true

function showHide(){
  isShow=!isShow
  userSection.classList.toggle("hide", isShow);
}

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

0

You should use window.event, from inside the event handler, to grab the event fired and fetch its target property to get the element firing the event.

Once you have the button that got clicked, you can use a strategy like getting its next sibling like I did here on this demo, and toggle the hide class.

function showHide(){
  const elementClicked = window.event.target;  
  const elementNextToClickedOne = elementClicked.nextElementSibling;    
  elementNextToClickedOne.classList.toggle("hide");
}
.toggle__button{
  cursor: pointer;
}  
  
.hide{
  display:none;
}
  <button onclick="showHide()" class="toggle__button">click</button>
  <h1 class="toggle__elements hide">1</h1>


  <button onclick="showHide()" class="toggle__button">click</button>
  <h1 class="toggle__elements hide">2</h1>


  <button onclick="showHide()" class="toggle__button">click</button>
  <h1 class="toggle__elements hide">3</h1>

about 4 years ago · Juan Pablo Isaza Denunciar

0

I've updated your function to work independently for each button. The comments inside the snippet should provide some information on how this is achieved. The most notable thing here is that we can use this in onclick="showHide(this)" to pass down the instance of our button element to the showHide function. Which allows us to select the correct sibling element and toggle its hide class.

function showHide(button){
  const userSection = button.nextSibling.nextElementSibling; // Select the next sibling of the clicked button
  userSection.classList.toggle("hide");
}
<button onclick="showHide(this)" class="toggle__button">click</button>
  <h1 class="toggle__elements hide">1</h1>


  <button onclick="showHide(this)" class="toggle__button">click</button>
  <h1 class="toggle__elements hide">2</h1>


  <button onclick="showHide(this)" class="toggle__button">click</button>
  <h1 class="toggle__elements hide">3</h1>


<style>
  .hide{
    display:none;
  }
</style>

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