I'm using the jQuery UI Accordion (which does not allow more than one item open at a time) on a project. Using accordion is appropriate since I usually do only want one panel open at a time.
However, I need to offer an "Expand All" link which switches to "Collapse All" when clicked. I don't want to custom write near-identical accordion functionality around this one requirement, so I'd like some JS that will achieve this whilst keeping the Accordion component in use.
Question: What JavaScript/jQuery is required to achieve this whilst still using the jQuery UI "Accordion" component to power the standard functionality?
Here's a fiddle: http://jsfiddle.net/alecrust/a6Cu7/
Using an example about for Taifun, I modified to allow expand and collapse.
... // hook up the expand/collapse all
This my solution:
Working in real project.
http://jsfiddle.net/bigvax/hEApL/
A lot of these seem to be overcomplicated. I achieved what I wanted with just the following:
JSFiddle
try this one jquery-multi-open-accordion, might help you
As discussed in the jQuery UI forums, you should not use accordions for this.
If you want something that looks and acts like an accordion, that is fine. Use their classes to style them, and implement whatever functionality you need. Then adding a button to open or close them all is pretty straightforward. Example
HTML
By using the jquery-ui classes, we keep our accordions looking just like the "real" accordions.
Roll your own accordions
Mostly we just want accordion headers to toggle the state of the following sibling, which is it's content area. We have also added two custom events "show" and "hide" which we will hook into later.
Expand/Collapse All
We use a boolean
isAllOpen
flag to mark when the button has been changed, this could just as easily have been a class, or a state variable on a larger plugin framework.Swap the button when "all open"
Thanks to our custom "show" and "hide" events, we have something to listen for when panels are changing. The only special case is "are they all open", if yes the button should be a "Collapse all", if not it should be "Expand all".
Edit for comment: Maintaining "1 panel open only" unless you hit the "Expand all" button is actually much easier. Example