JavaScript/CSS/HTML || Simple Collapsible Accordion Panel

Print Friendly, PDF & Email

Accordion panels are a vertical stack of items. Each item can be “expanded” or “collapsed” to reveal the content associated with that item. There can be zero expanded items, or more than one item expanded at a time, depending on the configuration.

Using JavaScript, the following is sample code which demonstrates how to display a simple collapsible accordion panel to the page.

This panel comes with a few basic features. Using data attributes, you can adjust the slide up and slide down speed, as well as the initial collapsed status. You can also specify either the entire row as selectable, or just the button.

REQUIRED KNOWLEDGE


JavaScript - closest
JavaScript - nextElementSibling
JavaScript - querySelector
JavaScript - querySelectorAll
JavaScript - stopPropagation
JavaScript - Events
JavaScript - getComputedStyle
CSS- transitions
Accordion Panel - What is it?

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Leave a Reply