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

148
Views
Creating an Accordion with JavaScript

Trying to open and close a accordion with JavaScript. This is what I have so far, but it doesn't work. I really can't find the mistake (I know there is one!!!)

const sContent = document.getElementsByClassName("content"),
  sHeader = document.querySelectorAll(".header");

function toggleSki() {
  let itemClass = this.parentNode.className;

  for (i = 0; i < sContent.length; i++) {
    sContent[i].className = "content close";
  }
  if (itemClass === "content close") {
    this.parentNode.className = "content open";
  }
}
sHeader.forEach((el) => {
  el.addEventListener("click", toggleSki);
});
.close .list {
  height: 0;
  overflow: hidden;
  transition: 0.3s;
}

.open .list {
  height: auto;
  margin-bottom: 2.5em;
  ;
}

.open .arrow {
  transform: rotate(-180deg);
}
<div class="content close">

  <div class="header">

    <div class="title">
      <h3>headline</h3>
      <span>Sub title</span>
    </div>

  </div>

  <div class="list grid">

    <div class="data">

      <div class="titles flex">
        <h4 class="name">ABC</h4>
        <p class="number">100%</p>
      </div>

      <div class="bar">
        <div class="perc bg"></div>
        <div class="bar_bg 4d"></div>
      </div>

    </div>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

in case your code still doesn't run on your environment. I think the problem might be that your for loop misses a variable declaration:

for (let i = 0; i < sContent.length; i++)

should solve the difficulties

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!