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

Frontend Mentor -- Responsive FAQ Accordion Design

@Tobi007-del

Desktop design screenshot for the FAQ accordion coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
1newbie
View challenge

Design comparison


SolutionDesign

Solution retrospective


What are you most proud of, and what would you do differently next time?

I am most proud of how precise my design is compared to the template i was given.

What challenges did you encounter, and how did you overcome them?

I'd say what was most challenging at the time of completion which was a while back was the javascript for making the plus switch with the minus on a click event since i was quite a newbie then.

What specific areas of your project would you like help with?

Well, I would like to receive feedback on how i did and what technologies i should be using to better my workspace.

Community feedback

Mahmood 1,070

@MahmoodHashem

Posted

Hello,There

Congratulations on successfully completing your challenge.

Your project is impressive.

I have some suggestions to further improve it:

  • The FAQ section should behave like an accordion, where opening one question hides the others. You can achieve this by querying all the paragraphs and using conditional logic in a foreach loop to hide or show them.

Here is the updated code, where some classes should be added to html elements like .toggle-button to plus and minus images and .question-answer to the p of answers

document.addEventListener("DOMContentLoaded", function() {
    const buttons = document.querySelectorAll(".toggle-button");
    const paragraphs = document.querySelectorAll(".question-answer");

    buttons.forEach((button, index) => {
        button.addEventListener("click", function() {
            paragraphs.forEach((p, i) => {
                if (i === index) {
                    p.classList.toggle('hide');
                    button.src = p.classList.contains('hide')
                        ? './assets/images/icon-plus.svg'
                        : './assets/images/icon-minus.svg';
                } else {
                    p.classList.add('hide');
                    buttons[i].src = './assets/images/icon-plus.svg';
                }
            });
        });
    });
});

Explanation:

  • This code adds an event listener to the document that waits for the DOM content to be fully loaded. Once loaded, it selects all elements with the class "toggle-button" and question-answer using document.querySelectorAll().

  • It then loops through each button using forEach() and adds a click event listener to each button. When a button is clicked, it loops through each paragraph and checks if the index of the paragraph matches the index of the clicked button. If they match, it toggles the hide class on the paragraph and changes the source of the button's image depending on whether the hide class is present or not.

  • If the indexes do not match, it adds the hide class to the paragraph and changes the source of the button's image to ./assets/images/icon-plus.svg.

Hope that's helpful!

Keep up the great work!

Keep up the hard work

Marked as helpful

0

@Tobi007-del

Posted

Thank you very much, i wasn't aware thats how an accordion should work but i get the concept now though...I appreciate your comment, you can check the updated result by the way, i have implemented your suggestion, thanks again.@MahmoodHashem

1
Mahmood 1,070

@MahmoodHashem

Posted

@Tobi007-del Good job, I will

0

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