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 Card

Anna P. M. 390

@annapmarin

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


This is my solution for this challenge 💫👩🏻‍💻

Built with: -SASS (my first time) -JavaScript (vanilla)

Any suggestions on how I can improve and reduce unnecessary code are welcome! (javascript in particular)

Thank you! 🙏🏻

Community feedback

@pablorodriguez-tk

Posted

To reduce unnecessary code on JS , you can do this. Add to every clickable item, the class "list-item" on html

Then, you can use this solution using JavaScript (vanilla). i add some comment to try to explain the code

// Get all the elements on the DOM with class "list-item"
const listItem = document.getElementsByClassName("list-item");

for (const item of listItem) {
  const onLabelClicked = () => {
    // if the clicked element has the active class, remove it
    if (item.classList.contains("active")) {
      item.classList.remove("active");
      return;
    }

    //remove active class from all elements
    if (document.querySelector(".active")) {
      document.querySelector(".active").classList.remove("active");
    }

    // add active class to the clicked element
    item.classList.add("active");
  };

  item.addEventListener("click", onLabelClicked);
}

You can see my code solution here GITHUB-LINK

Marked as helpful

1

Anna P. M. 390

@annapmarin

Posted

@pablorodriguez-tk Thanks!!

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