I'm following this tutorial on how to make my own collapsible using html, css, and a bit of javascript. So far, I've gotten my desired output with code like this:
var question = document.getElementsByClassName("question");
var i;
for (i = 0; i < question.length; i++) {
question[i].addEventListener("click", function() {
this.classList.toggle("active");
var answer = this.nextElementSibling.nextElementSibling;
if (answer.style.maxHeight){
answer.style.maxHeight = null;
} else {
answer.style.maxHeight = answer.scrollHeight + "px";
}
});
}
.faq {
width: 100%
}
.question {
background-color: transparent;
color: #329d9c;
cursor: pointer;
padding: 2% 10%;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 18px;
font-weight: 700;
}
.question:after {
content: "\276F"; /*up*/
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(-90deg);
}
.active:after {
content: "\276F"; /*down*/
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(90deg);
}
.line {
width: 90%;
height: 1%;
background-color: #dddddd;
border-radius: 30px;
position: absolute;
margin-left: 10%;
}
.answer {
margin-left: 10%;
padding: 0 6%;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
background-color: white;
box-shadow: 1px 2px #929191;
border: none;
border-radius: 0 0 10px 10px;
color: black;
}
<div class="faq">
<button class="question">Question 1</button>
<div class="line"></div>
<div class="answer">
Answer 1
</div>
<button class="question">Question 2</button>
<div class="line"></div>
<div class="answer">
Answer 2
</div>
</div>
I tried to run the same code and make collapsibles on multiple tabs, so it looks more or less like this:
<div class="tabs">
<div class="tab-1">
<!-- the same code as snippet --!>
</div>
<div class="tab-2">
<!-- the same code as snippet --!>
</div>
</div>
But it's not working so far. I tried using bootstrap 3 to use the accordion but it messes up my custom css styling, so I wonder is there any way I could work around this issue?
const topic = document.getElementsByClassName('topic');
const topicQS = document.querySelectorAll('.topic');
let topicQSArray = [];
//console.log('keysQS', Object.keys(topicQS));
for (let i = 0; i < topicQS.length; i++) {
topicQS[i].addEventListener('click', function showQuestionQS(e) {
this.classList.toggle('active');
this.nextElementSibling.classList.toggle('show');
let questionsQS = document.querySelectorAll('question');
});
}
const question = document.getElementsByClassName('question');
for (let i = 0; i < question.length; i++) {
question[i].addEventListener('click', function () {
this.classList.toggle('active');
const answer = this.nextElementSibling.nextElementSibling;
if (answer.style.maxHeight) {
answer.style.maxHeight = null;
} else {
answer.style.maxHeight = (20 + answer.scrollHeight) + 'px';
}
});
}
.topic {
background-color: transparent;
color: #329d9c;
cursor: pointer;
padding: 2% 10%;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 18px;
font-weight: 700;
}
.topic::after {
content: '\276F';
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(-90deg);
}
.question {
position: relative;
left: 50px;
display: block;
background-color: transparent;
color: #329d9c;
cursor: pointer;
padding: 2% 10%;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 18px;
font-weight: 700;
}
.question::after {
content: '\276F';
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(-90deg);
}
.question-container {
/* transition: all ease-in-out 0.3s; */
display: none;
}
.active::after {
content: '\276F';
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(90deg);
}
.line {
width: 90%;
height: 1%;
background-color: #dddddd;
border-radius: 30px;
position: absolute;
margin-left: 10%;
}
.answer {
margin-left: 10%;
margin-top: 1rem;
padding: 0 6%;
max-height: 0;
overflow: auto;
transition: max-height 0.2s ease-in-out;
background-color: white;
box-shadow: 1px 2px #929191;
border: none;
border-radius: 0 0 10px 10px;
color: black;
}
.show {
display: block;
}
<h1>Collapsed Q & A</h1>
<div class="topic1">
<button class="topic">Topic 1</button>
<div class="question-container">
<button class="question">Question 1</button>
<div class="line"></div>
<div class="answer">Answer 1 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
<button class="question">Question 2</button>
<div class="line"></div>
<div class="answer">Answer 2 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
<button class="question">Question 3</button>
<div class="line"></div>
<div class="answer">Answer 3 Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
</div>
</div>
<div class="topic2">
<button class="topic">Topic 2</button>
<div class="question-container">
<button class="question">Question 4</button>
<div class="line"></div>
<div class="answer">Answer 4</div>
<button class="question">Question 5</button>
<div class="line"></div>
<div class="answer">Answer 5</div>
<button class="question">Question 6</button>
<div class="line"></div>
<div class="answer">Answer 6</div>
</div>
</div>
<div class="topic3">
<button class="topic">Topic 3</button>
<div class="question-container">
<button class="question">Question 7</button>
<div class="line"></div>
<div class="answer">Answer 7</div>
<button class="question">Question 8</button>
<div class="line"></div>
<div class="answer">Answer 8</div>
<button class="question">Question 9</button>
<div class="line"></div>
<div class="answer">Answer 9</div>
</div>
</div>