FAQ Accordion Card with vanilla HTML/CSS/JS

Solution retrospective
How can I fix the issue of it taking two double clicks to open a separate dropdown?
Please log in to post a comment
Log in with GitHubCommunity feedback
- @vanzasetia
Hi! 👋
I notice in the console that every time I want to open another accordion panel, at first the
flag
isfalse
and the on the second click it gives metrue
and opens the accordion panel.I can't understand the issue. However, I suggest using the native HTML element for the accordion. By default, it's accessible by keyboard users and screen reader users. Also, it has built-in open and close functionality. So for JavaScript, all you have to do is to create a function where it only allows one accordion to be open at a time.
I recommend using
forEach
instead offor
loop. It's much shorter and easier to understand than the traditionalfor
loop.Avoid using JavaScript to add styling (unless you've no other option). JavaScript allows you to change the CSS code using the
style
property. But, to make debugging easier and improve code maintainability, it’s best to avoid it. Use CSS classes instead.I recommend using all classes begin with
js-
to select DOM elements within the JavaScript. Keep in mind, that only use thejs-
classes only for JavaScript purposes.// Example const form = document.querySelector(".js-form");
Lastly, I notice that you use the
raw.githubusercontent.com
file path for the image instead of using./images
. Why do you change it to an absolute file path?Hope this helps.
Marked as helpful
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