Design comparison
Solution retrospective
I was struggling with this one. Any advice is greatly appreciated!
Community feedback
- @CarlHummPosted about 1 year ago
Hi there, nice try
Your JS doesn't work as accordionContent is a nodelist, not an element
You are attempting to toggle a class on accordionContent which, like accordionHeadings, is a nodeList and not an element. For each click, we instead want to reference the accordion-content relative to the clicked element. How do we do that?
Here are just two common options.
i.nextElementSibling.classList.toggle('hidden')
i.parentElement.querySelector('.accordian-content').classList.toggle('hidden')
accordianHeadings.forEach(function (i) { i.addEventListener("click", function () { // Replace me }) })
So
i
references the clicked element (the heading). Working from the heading we can either go up one to the parent and search for accordion-content, or use the nextElementSibling property. Since our accordion is composed of headings and content, the nextElementSibling will always be theaccordion-content
which works well for us.Alternatively you could use the HTML Details & Summary Elements
<details> <summary>Accordion Heading</summary> Something small enough to escape casual notice. </details>
Unfortunately I only had time to write a solution for the JS. Perhaps someone else can help with the layout and images.
Good luck on your future projects
Marked as helpful1
Please log in to post a comment
Log in with GitHubJoin 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