Design comparison
Solution retrospective
Please review my JS code and let me know if I am over coding. If there is a simpler and cleaner way to code I would like to know.
I would like to know how you would handle the issue of removing previous class ,active from the DOM when dealing with spacebar key event.
What feature would make this solution pop and stand out?
Community feedback
- @bbsmoothdevPosted 11 months ago
I think you've over-engineered the keyboard handling. You've hijacked the up and down arrows so I can't scroll the page, and I can't even use the Tab key to get to the links at the bottom of the page. This is called a keyboard trap. Also, most keyboard users are going to assume that they can use the Tab key to navigate through each FAQ item, so you've broken the expected behavior here.
My recommendation would be to use an HTML
button
element for each of the FAQ titles. Then you only need to add a click handler to them and you can forget about all the mouseover and keydown handlers you've added. This will allow keyboard users to Tab through each FAQ title and use either the Space bar or Enter key to expand them. There are some aria attributes you'll need to add to those buttons as well and toggle their values using JS. For an example of how to do this, see Tutorial: Let's Build an Accessible Disclosure.Marked as helpful1@vcgmusePosted 11 months ago@bbsmoothdev Your suggestions really helped. I'm reading more about the article you sent me and It's good stuff. Thanks for making my code better.
1
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