I have two accordions one inside the other: the main one is a category, the second one is a question/answer pair.
My problem is that when I click a question, the overall maxHeight of the Category remains the same as before, forcing my Q&A pairs to overflow, instead of adding to the maxHeight of the Category. I am very new with Js, so I am having a hard time understanding what I am missing. All my attempts at fiddling with the js code have failed.
Here is a codePen of what I am dealing with: https://codepen.io/iuliard/pen/KKMzMLe
////ACCORDION////
var accordions1 = document.getElementsByClassName("category_name");
for (var i = 0; i < accordions1.length; i++) {
accordions1[i].onclick = function() {
this.classList.toggle('is-open');
var content1 = this.nextElementSibling;
if (content1.style.maxHeight) {
// accordion is currently open, so close it
content1.style.maxHeight = null;
} else {
// accordion is currently closed, so open it
content1.style.maxHeight = content1.scrollHeight + "px";
}
}
}
var accordions2 = document.getElementsByClassName("question");
for (var i = 0; i < accordions2.length; i++) {
accordions2[i].onclick = function() {
this.classList.toggle('is-open');
var content2 = this.nextElementSibling;
if (content2.style.maxHeight) {
// accordion is currently open, so close it
content2.style.maxHeight = null;
} else {
// accordion is currently closed, so open it
content2.style.maxHeight = content2.scrollHeight + "px";
}
}
}
Thank you in advance for your help!