I want to be able to click on the accordion question and have the arrow rotate 180 degrees up and then back down if I again click on the question.
I already figured out how to expand the accordion answer on click using javascript so I just have the arrow left.
Can anyone assist?
Here is the html, css, and js code.
`
<div >
<div >
<button>
<h3>Which services do you provide?</h3>
<i ></i>
</button>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing 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. </p>
</div>
</div>
`
`
.faqs-container {
max-width: 60%;
margin: 0 auto;
}
.faqs-question {
width: 100%;
border-bottom: 3px solid #999;
margin-bottom: 30px;
}
.faqs-question button{
width: 100%;
background-color: white;
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px 15px;
border: none;
outline: none;
font-size: 24px;
font-family: 'interstate', sans-serif;
font-weight: 500;
color: #300600;
cursor: pointer;
transition: 0.6s ease-in-out;
}
.faqs-question button h3{
font-size: 24px;
font-family: 'interstate', sans-serif;
font-weight: 500;
}
.faqs-question i{
font-size: 48px;
}
.faqs-question p {
margin-top: 10px;
font-size: 24px;
font-family: 'Merriweather-Sans', sans-serif;
font-weight: 300;
line-height: 55px;
color: #444;
max-height: 0;
opacity: 0;
overflow: hidden;
transition: 0.6s ease;
}
.faqs-question p.show{
max-height: 30vh;
opacity: 1;
padding: 10px 0px 40px 15px;
margin-bottom: 35px;
}
`
`
<script>
const buttons = document.querySelectorAll('button');
buttons.forEach( button =>{
button.addEventListener('click',()=>{
const faq = button.nextElementSibling;
faq.classList.toggle('show');
})
} )
</script>
`
I don't know where to start with the javascript. I'm still fairly new with all of this
CodePudding user response:
You'll have to toggle a class on the icon as well:
const buttons = document.querySelectorAll('button');
buttons.forEach(button => {
button.addEventListener('click', () => {
const faq = button.nextElementSibling;
faq.classList.toggle('show');
button.querySelector('i').classList.toggle('rotate')
})
})
.faqs-container {
max-width: 60%;
margin: 0 auto;
}
.faqs-question {
width: 100%;
border-bottom: 3px solid #999;
margin-bottom: 30px;
}
.faqs-question button {
width: 100%;
background-color: white;
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px 15px;
border: none;
outline: none;
font-size: 24px;
font-family: 'interstate', sans-serif;
font-weight: 500;
color: #300600;
cursor: pointer;
transition: 0.6s ease-in-out;
}
.faqs-question button h3 {
font-size: 24px;
font-family: 'interstate', sans-serif;
font-weight: 500;
}
.faqs-question i {
transition: 0.2s;
font-size: 48px;
}
.faqs-question p {
margin-top: 10px;
font-size: 24px;
font-family: 'Merriweather-Sans', sans-serif;
font-weight: 300;
line-height: 55px;
color: #444;
max-height: 0;
opacity: 0;
overflow: hidden;
transition: 0.6s ease;
}
.faqs-question p.show {
max-height: 30vh;
opacity: 1;
padding: 10px 0px 40px 15px;
margin-bottom: 35px;
}
.rotate {
transform: rotate(90deg);
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE 4aHK8yyUHUSCcJHgXloTyT2A==" crossorigin="anonymous" referrerpolicy="no-referrer"
/>
<div >
<div >
<button>
<h3>Which services do you provide?</h3>
<i ></i>
</button>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing 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. </p>
</div>
</div>