Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted about 1 year ago

FAQ Accordion with HTML, CSS and JS

Vasiliki-Georgiou•290
@Vasiliki-Georgiou
A solution to the FAQ accordion challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


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

I have an issue with the accordion, sometimes the plus and minus buttons bug and I have to reload the page for them to function again.

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Vasiliki-Georgiou•290
    @Vasiliki-Georgiou
    Posted about 1 year ago

    Thank you very much for your help. I tried your proposed solution, but I could not make it entirely work with my code, but I found another way to resolve the bug of the buttons not having the desired behavior if a user clicked on them repeatedly.

    Have a nice day :-)

  • Vinayak•320
    @V102002
    Posted about 1 year ago

    Instead of : let minusSvg = '<svg xmlns="http://www.w3.org/2000/svg" width="30" height="31" fill="none" viewBox="0 0 30 31"><path fill="#301534" d="M15 3.313A12.187 12.187 0 1 0 27.188 15.5 12.2 12.2 0 0 0 15 3.312Zm4.688 13.124h-9.375a.938.938 0 0 1 0-1.875h9.374a.938.938 0 0 1 0 1.876Z"/></svg>';

    Why not try:

    let minusSvg=document.querySelector("name of the image class"); And for plusSvg as well.

    For the accordians, we have to access each of them, so use a forEach loop access the icons and change them in the conditional statements It will look something like this

    const accordions = document.querySelectorAll('.accordion'); // get the accordion const backdrop = document.querySelector('.backdrop');

    accordions.forEach(accordion => { // access the answer and the image from each accordion const icon = accordion.querySelector(".icon"); const ans = accordion.querySelector(".answer");

    // adding a click eventListener to each accordion
    accordion.addEventListener("click", () => {
        ans.classList.toggle("active"); // adding the active class based on toggle
    
        // check if the accordion has been clicked
        if (ans.classList.contains("active")) {
            icon.src = "./assets/images/icon-minus.svg";
        } else {
            icon.src = "./assets/images/icon-plus.svg";
        }
    });
    

    }); Hope this helps!

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

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit all CSS, SCSS and Less files in your repository.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

How does the JavaScript validation report work?

When a solution is submitted, we use eslint to run an automated check on the JavaScript code.

The report picks out common JavaScript issues such as not using semicolons and using var instead of let or const, among others.

The report will audit all JS and JSX files in your repository. We currently do not support Typescript or other frontend frameworks.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub