Create a Expand/Collapse FAQ Accordion, Collapse other on Click
This guide creates a FAQ expand/collapse javascript question and answer set up whereby when another question is expanded the current open answer is collapsed. So only one is expanded at any point. In the example below click to see how it works.
I have changed it to better work in the WordPress post editor – but can be used in any CMS or web application.
Example 1 – FAQs
Q1. What currency is the course charged in?
Q2. What if the course doesn’t help me?
Q3. When will the webinars take place?
Q4. What is the self-directed mentoring program?
The HTML
In the HTML markup there are 2 key CSS classes accordion and panel, I have used the p tag for the question and it is given the accordion class and the answer is wrapped in a div with the panel class.
The CSS
The .panel class is set to be hidden initially via the opacity and max-height CSS values and is set to show when it is given the .show class via javascript.
The Javascript
Leaving the toggling to the user
If you don’t care about only viewing one panel you can toggle all accordions independently, here the user has the onus to collapse back the panel, just swap the javascript, as below…
Example 2 – FAQs
Q1. What currency is the course charged in?
Q2. What if the course doesn’t help me?
Q3. When will the webinars take place?
Q4. What is the self-directed mentoring program?
Ref
http://stackoverflow.com/questions/37745154/only-open-one-accordion-tab-at-one-time
http://www.w3schools.com/howto/howto_js_accordion.asp
The example I have used, uses different HTML elements than the w3schools example, the reason is more WordPress post editor friendly as the w3schools uses the button element which can get enclosed by a p element in WordPress which then will fail the javascript.