Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!