Accordion
jQuery UI Accordion
Overview
Make the selected elements Accordion widgets. Semantic requirements:
The markup of your accordion container needs pairs of headers and content panels:
<div id="accordion"> <h3><a >First header</a></h3> <div>First content</div> <h3><a >Second header</a></h3> <div>Second content</div> </div>
If you use a different element for the header, specify the header-option with an appropriate selector, eg. header: 'a.header'. The content element must be always next to its header.
If you have links inside the accordion content and use a-elements as headers, add a class to them and use that as the header, eg. header: 'a.header'.
Use activate(Number) to change the active content programmatically.
NOTE: If you want multiple sections open at once, don't use an accordion
An accordion doesn't allow more than one content panel to be open at the same time, and it takes a lot of effort to do that. If you are looking for a widget that allows more than one content panel to be open, don't use this. Usually it can be written with a few lines of jQuery instead, something like this:
jQuery(document).ready(function(){ $('.accordion .head').click(function() { $(this).next().toggle(); return false; }).next().hide(); });
Or animated:
jQuery(document).ready(function(){ $('.accordion .head').click(function() { $(this).next().toggle('slow'); return false; }).next().hide(); });
Dependencies
- UI Core
- UI Widget
- UI Effects Core (Optional - only for non-default animations)
Example
$("#accordion").accordion();
<!DOCTYPE html> <html> <head> <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> <script> $(document).ready(function() { $("#accordion").accordion(); }); </script> </head> <body style="font-size:62.5%;"> <div id="accordion"> <h3><a >Section 1</a></h3> <div> <p> Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate. </p> </div> <h3><a >Section 2</a></h3> <div> <p> Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit faucibus urna. </p> </div> <h3><a >Section 3</a></h3> <div> <p> Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis. Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui. </p> <ul> <li>List item one</li> <li>List item two</li> <li>List item three</li> </ul> </div> <h3><a >Section 4</a></h3> <div> <p> Cras dictum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel est. </p> <p> Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. </p> </div> </div> </body> </html>
Options
Events
Methods
-
destroy
- Signature:
- .accordion( "destroy" )
Remove the accordion functionality completely. This will return the element back to its pre-init state.
-
disable
- Signature:
- .accordion( "disable" )
Disable the accordion.
-
enable
- Signature:
- .accordion( "enable" )
Enable the accordion.
-
option
- Signature:
- .accordion( "option" , optionName , [value] )
Get or set any accordion option. If no value is specified, will act as a getter.
-
option
- Signature:
- .accordion( "option" , options )
Set multiple accordion options at once by providing an options object.
-
widget
- Signature:
- .accordion( "widget" )
Returns the .ui-accordion element.
-
activate
- Signature:
- .accordion( "activate" , index )
Activate a content part of the Accordion programmatically. The index can be a zero-indexed number to match the position of the header to close or a Selector matching an element. Pass
false
to close all (only possible withcollapsible:true
). -
resize
- Signature:
- .accordion( "resize" )
Recompute heights of the accordion contents when using the fillSpace option and the container height changed. For example, when the container is a resizable, this method should be called by its resize-event.
Theming
The jQuery UI Accordion plugin uses the jQuery UI CSS Framework to style its look and feel, including colors and background textures. We recommend using the ThemeRoller tool to create and download custom themes that are easy to build and maintain.
If a deeper level of customization is needed, there are widget-specific classes referenced within the jquery.ui.accordion.css stylesheet that can be modified. These classes are highlighed in bold below.
Sample markup with jQuery UI CSS Framework classes
<div class="ui-accordion ui-widget ui-helper-reset"><h3 class="ui-accordion-header ui-helper-reset ui-state-active ui-corner-top">
<span class="ui-icon ui-icon-triangle-1-s"/>
<a >Section 1</a>
</h3>
<div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active">
Section 1 content
</div>
<h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-corner-all">
<span class="ui-icon ui-icon-triangle-1-e"/>
<a >Section 2</a>
</h3>
<div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom">
Section 2 content
</div>
<h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-corner-all">
<span class="ui-icon ui-icon-triangle-1-e"/>
<a >Section 3</a>
</h3>
<div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom">
Section 3 content
</div>
</div>
Note: This is a sample of markup generated by the accordion plugin, not markup you should use to create a accordion. The only markup needed for that is
<div>
<h3><a >Section 1</a></h3>
<div>
Section 1 content
</div>
<h3><a >Section 2</a></h3>
<div>
Section 2 content
</div>
<h3><a >Section 3</a></h3>
<div>
Section 3 content
</div>
</div>.