This is a solution to the FAQ accordion challenge on Frontend Mentor. Frontend Mentor challenges help you improve your coding skills by building realistic projects.
Users should be able to:
- Hide/Show the answer to a question when the question is clicked
- Navigate the questions and hide/show answers using keyboard navigation alone
- View the optimal layout for the interface depending on their device's screen size
- See hover and focus states for all interactive elements on the page
- Solution URL: Frontend Mentor
- Live Site URL: Github Pages
To switch the icons I used the JavaScript code below. I tried to used the btn.insertAdjacientHTML("afterend", minusIcon)
but the minus icon would be outside the button
completely. I decided to stick to beforeend
instead and implemented this code below:
[aria-expanded="true"] .add {
display: none;
}
.minus {
display: none;
}
[aria-expanded="true"] .minus {
display: block;
}
btn.insertAdjacentHTML("beforeend", plusIcon);
btn.insertAdjacentHTML("beforeend", minusIcon);
- WAI Accordion Example - I used this accordion pattern in this project.