--- group: javascript-developer-guide subgroup: 3_Widgets title: Accordion widget --- ## Overview Magento accordion widget is an [extension](https://glossary.magento.com/extension) of the [Magento Tabs widget]. Accordions are generally used to break content into multiple sections that can be swapped to save space. The accordion [widget](https://glossary.magento.com/widget) source is [lib/web/mage/accordion.js]. ## Initialize the accordion widget {#accordion_init} ### Initialize accordion in JS components #### Initialize accordion with `data-*` attributes specified Generally the accordion widget is instantiated like following: ```javascript $("#element").accordion(); ``` Where: - `#element` is the selector of the element for accordion is initialized. - `#element` has children with the following attributes specified: - `data-role="title"` - `data-role="content"` Optionally, you can specify the following: - If you want the trigger to be different from the title, add the `data-role="content"` attribute for the element - To have the content updated using Ajax, add the `data-ajax="true"` attribute for the element containing the URL for request. Accordions support arbitrary markup, but the following requirements should be kept: 1. Titles and contents are specified in the same order in DOM: first title, then contents. 2. The header, trigger and content are specified, either by adding the `data-*` attributes for the corresponding children elements or by specifying these elements with selectors as options. Mark-up examples: ```html
Title 1
Content 1
Title 2
Content 2
Title 3
Content 3
``` #### Initialize accordion with option You can specify the header, content, trigger as options when you initialize the widget. For example: ```javascript $("#element").accordion({ header : "#title-1", content : "#content-1", trigger : "#trigger-1", ajaxUrlElement: "a" }); ``` ### Initialize accordion in a template The accordion widget can be initialized using the `data-mage-init` attribute or `