title: How to close accordion panels when another one opens?
body: <p>I have accordion widget on my static HTML/CSS/JS site.</p>
  
  <p>By default all accordions are closed.</p>
  
  <p>User can open any of them and those will stay opened until the user manually closes them by clicking on the accordion title.</p>
  
  <p>How can I make previously opened accordions to close when users click to open another one?</p>
  
  <p>I created my accordion using this template: <a href="https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_accordion_animate" rel="nofollow noreferrer">https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_accordion_animate</a></p>
  
  <p>I put both HTML and JS in the HTML file:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var acc = document.getElementsByClassName("accordion");
  var i;
  
  for (i = 0; i &lt; acc.length; i++) {
  acc[i].addEventListener("click", function() {
  this.classList.toggle("active");
  var panel = this.nextElementSibling;
  if (panel.style.maxHeight){
    panel.style.maxHeight = null;
  } else {
    panel.style.maxHeight = panel.scrollHeight + "px";
  }
  });
  }</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.accordion {
    color: #fff;
    background-color: #00000042;
    cursor: pointer;
    padding: 10px 10px;
    margin-top:20px;
    width: 100%;
    border: 1px solid #00000042;
    text-align: left;
    outline: none;
    transition: 0.4s;
  }
  
  .active, .accordion:hover {
    background-color: #00000042;
    border: 1px solid #fc8e2d;
  }
  
  .panel {
    padding: 0 18px;
    font-size: 18px;
    background-color: #00000042;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease-out;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;button class="accordion"&gt;&lt;span class="faq__question-heading"&gt;Title1&lt;/span&gt;&lt;/button&gt;
  &lt;div class="panel"&gt;
    &lt;p style="padding:18px 0;"&gt;description1&lt;/p&gt;
  &lt;/div&gt;
  
  &lt;button class="accordion"&gt;&lt;span class="faq__question-heading"&gt;Title2&lt;/span&gt;&lt;/button&gt;
  &lt;div class="panel"&gt;
    &lt;p style="padding:18px 0;"&gt;description2&lt;/p&gt;
  &lt;/div&gt;
  
  &lt;button class="accordion"&gt;&lt;span class="faq__question-heading"&gt;Title3&lt;/span&gt;&lt;/button&gt;
  &lt;div class="panel"&gt;
    &lt;p style="padding:18px 0;"&gt;description3&lt;/p&gt;
  &lt;/div&gt;
  
  &lt;script&gt;
  
  &lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>