0

I have created a nested accordion using HTML/CSS and javascript. I have multiple main accordions with 2 sub-accordions in each. My intended result is to expand the sub-accordions and have the next main accordion shift down, however the actual result is that the main accordion does not move down and the sub-accordions aren't expanding properly.

If you run my fiddle example, clicking the first sub-accordion pushes down the second sub-accordion but doesn't push Main#2, and the second sub-accordion doesn't expand on either Main#1 or Main#2.

If someone could help point me in the right direction here I'd really appreciate it! Thanks!

Link to fiddle

HTML:

<div class="collapsible">
  <h3><strong>Main#1</strong></h3>
</div>
  <div class="content">
    <div class="collapsible">
      <h4>Sub#1</h4>
    </div>
    <div class="content">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
    </div>
    <div class="collapsible">
    <h4>Sub#2</h4>
  </div>
  <div class="content">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
  </div>
</div>

<div class="collapsible">
  <h3><strong>Main#2</strong></h3>
</div>
<div class="content">
  <div class="collapsible">
    <h4>Sub#1</h4>
  </div>
  <div class="content">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
  </div>
  <div class="collapsible">
    <h4>Sub#2</h4>
  </div>
  <div class="content">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
  </div>
</div>

CSS:

.collapsible {
  background-color: white;
  color: black;
  cursor: pointer;
  padding: 1vw;
  width:97%;
  height: 60px;
  max-height: 100%;
  margin: 5px 10px 5px 10px;
  border: 2px solid black;
  text-align: left;
  outline: none;
  font-size: 25px;
}

.content {
  padding: 0 18px;
  max-height: 0;
  overflow: hidden;
  background-color: white;
  transition: max-height 0.2s ease-out;
}

h3{
  display:inline-block;
  vertical-align: middle;
}

h4{
  display:inline-block;
  vertical-align: middle;
  font-size: 22px;
}

Javascript:

$(function() {
  var coll = document.getElementsByClassName("collapsible");

  //setting buttons of class 'collapsible' to extend on click
  for (i = 0; i < coll.length; i++) {
    coll[i].addEventListener("click", function () {
      this.classList.toggle("active");
      var content = this.nextElementSibling;
      if (content.style.maxHeight) {
        content.style.maxHeight = null;
      } else {
          content.style.maxHeight = content.scrollHeight + "px";
      }
    });
  }
});
Orion96
  • 23
  • 1
  • 5

1 Answers1

0

I made some changes in your code.

I add this part to your CSS:

.collapsible ~ .content{
  height: 0px;
}

.collapsible.active + .content{
  height: 100%;
}

Commented the 'max-height' line:

.content {
  padding: 0 18px;
  overflow: hidden;
  /*max-height: 0;*/
  background-color: white;
  transition: max-height 0.2s ease-out;
}

And simplify the JS:

$(function() {
  var coll = document.getElementsByClassName("collapsible");

  //setting buttons of class 'collapsible' to extend on click
  for (i = 0; i < coll.length; i++) {
    coll[i].addEventListener("click", function () {
      this.classList.toggle("active");
    });
  }
});

Here you can check the changes: https://jsfiddle.net/xpvt214o/501924/

With this, the hide and show is controlled by CSS.

Hope this help you.

Bruno Fortes
  • 1
  • 1
  • 1
  • This is helpful thank you! Would it be easy to add a slide animation to this? – Orion96 Jul 30 '18 at 12:56
  • Sorry, i forgot the transition part. Unfortunately css transition doesn't work with `height 100%` or `auto`. You could change the css to `max-length`, and set a high value like `500px`(but it's not a elegant way). I found this [post](https://stackoverflow.com/questions/3508605/how-can-i-transition-height-0-to-height-auto-using-css) with another ways to do the transition in a more elegant way – Bruno Fortes Jul 30 '18 at 15:23