Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

FAQ Accordion made using SASS, mobile first and JS.

@AdamAlberty

Desktop design screenshot for the FAQ accordion card coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
1newbie
View challenge

Design comparison


SolutionDesign

Solution retrospective


How should I implement faq expanding differently? I think that my solution is really messy.

Community feedback

@joanpujol

Posted

First of all, great-looking solution!

On the JS side, one way to simplify your code would be to use a click listener for each item that toggles an active class for that item.

faqItems.forEach(item => {
    item.addEventListener("click", () => {
       item.classList.toggle("active");
    });
 }); 

I hope it helps.

1

@AdamAlberty

Posted

@joanpujol Thanks a lot man! I didn't even think somebody would actually look at my stuff. I tried to make it that way, but couldn't figure it out. Another thing is that I wanted to make it so that when I click one item (to show the FAQ), the other ones hide.

PS: Sorry I replied this late.

1

@joanpujol

Posted

@AdamAlberty No problem! :)

Regarding the second problem. If you just want one item active at the time, you can first look for any active item, toggle it off, and then activate the selected item.

faqItems.forEach(item => {
    item.addEventListener("click", () => {
       const activeItem = document.querySelector(".active");
       activeItem.classList.toggle("active");
       item.classList.toggle("active");
    });
 })

I hope it helps!

1

Please log in to post a comment

Log in with GitHub
Discord logo

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord