JqueryUI Interactions
- JqueryUI - Sortable
- JqueryUI - Selectable
- JqueryUI - Resizable
- JqueryUI - Droppable
- JqueryUI - Draggable
JqueryUI Widgets
- JqueryUI - Tooltip
- JqueryUI - Tabs
- JqueryUI - Spinner
- JqueryUI - Slider
- JqueryUI - Progressbar
- JqueryUI - Menu
- JqueryUI - Dialog
- JqueryUI - Datepicker
- JqueryUI - Button
- JqueryUI - Autocomplete
- JqueryUI - Accordion
JqueryUI Effects
- JqueryUI - Toggle Class
- JqueryUI - Toggle
- JqueryUI - Switch Class
- JqueryUI - Show
- JqueryUI - Remove Class
- JqueryUI - Hide
- JqueryUI - Effect
- JqueryUI - Color Animation
- JqueryUI - Add Class
JqueryUI Utilities
JqueryUI Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
JqueryUI - Accordion
Accordion Widget in jQueryUI is a jQuery based expandable and collapsible content holder that is broken into sections and probably looks pke tabs. jQueryUI provides accordion() method to achieve this.
Syntax
The accordion() method can be used in two forms −
Method
Method
$ (selector, context).accordion (options) Method
The accordion (options) method declares that an HTML element and its contents should be treated and managed as accordion menus. The options parameter is an object that specifies the appearance and behavior of the menu involved.
Syntax
$(selector, context).accordion (options);
You can provide one or more options at a time using Javascript object. If there are more than one options to be provided then you will separate them using a comma as follows −
$(selector, context).accordion({option1: value1, option2: value2..... });
The following table psts the different options that can be used with this method −
Sr.No. | Option & Description |
---|---|
1 | Indicates the index of the menu that is open when the page is first accessed. By default its value is 0, i.e the first menu. |
2 | This option is used to set how to animate changing panels. By default its value is {}. |
3 | This option when set to true, it allows users to close a menu by cpcking on it. By default, cpcks on the open panel’s header have no effect. By default its value is false. |
4 | This option when set to true disables the accordion. By default its value is false. |
5 | This option specifies the event used to select an accordion header. By default its value is cpck. |
6 | This option specifies a selector or element to override the default pattern for identifying the header elements. By default its value is > p > :first-child,> :not(p):even. |
7 | This option is used to control the height of accordion and panels. By default its value is auto. |
8 | This option is an object that defines the icons to use to the left of the header text for opened and closed panels. The icon to use for closed panels is specified as a property named header, whereas the icon to use for open panels is specified as a property named headerSelected. By default its value is { "header": "ui-icon-triangle-1-e", "activeHeader": "ui-icon-triangle-1-s" }. |
The following section will show you a few working examples of accordion widget functionapty.
Default Functionapty
The following example demonstrates a simple example of accordion widget functionapty, passing no parameters to the accordion() method.
<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Accordion Example </title> <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <script> $(function() { $( "#accordion-1" ).accordion(); }); </script> <style> #accordion-1{font-size: 14px;} </style> </head> <body> <span id = "accordion-1"> <h3>Tab 1</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut apquip ex ea commodo consequat. </p> </span> <h3>Tab 2</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut apquip ex ea commodo consequat. </p> </span> <h3>Tab 3</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut apquip ex ea commodo consequat. </p> </span> </span> </body> </html>
Let us save the above code in an HTML file accordionexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −
Cpck headers(Tab 1,Tab 2,Tab 3) to expand/collapse content that is broken into logical sections, much pke tabs.
Use of collapsible
The following example demonstrates the usage of three options collapsible in the accordion widget of JqueryUI.
<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Accordion Example </title> <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <script> $(function() { $( "#accordion-2" ).accordion({ collapsible: true }); }); </script> <style> #accordion-2{font-size: 14px;} </style> </head> <body> <span id = "accordion-2"> <h3>Tab 1</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut apquip ex ea commodo consequat. </p> </span> <h3>Tab 2</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut apquip ex ea commodo consequat. </p> </span> <h3>Tab 3</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut apquip ex ea commodo consequat. </p> <ul> <p>List item one</p> <p>List item two</p> <p>List item three</p> </ul> </span> </span> </body> </html>
Let us save the above code in an HTML file accordionexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −
Here we have set collapsible to true. Cpck on an accordion header, this allows collapsing the active section.
Use of heightStyle
The following example demonstrates the usage of three options heightStyle in the accordion widget of JqueryUI.
<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Accordion Example </title> <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <script> $(function() { $( "#accordion-3" ).accordion({ heightStyle: "content" }); $( "#accordion-4" ).accordion({ heightStyle: "fill" }); }); </script> <style> #accordion-3, #accordion-4{font-size: 14px;} </style> </head> <body> <h3>Height style-content</h3> <span style = "height:250px"> <span id = "accordion-3"> <h3>Tab 1</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. </p> <ul> <p>List item one</p> <p>List item two</p> <p>List item three</p> <p>List item four</p> <p>List item five</p> </ul> </span> <h3>Tab 2</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. </p> </span> <h3>Tab 3</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. </p> </span> </span> </span><br><br> <h3>Height style-Fill</h3> <span style = "height:250px"> <span id = "accordion-4"> <h3>Tab 1</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. </p> <ul> <p>List item one</p> <p>List item two</p> <p>List item three</p> <p>List item four</p> <p>List item five</p> </ul> </span> <h3>Tab 2</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. </p> </span> <h3>Tab 3</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. </p> </span> </span> </span> </body> </html>
Let us save the above code in an HTML file accordionexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −
Here we have two accordions, the first one has heightStyle option set to content, which allows the accordion panels to keep their native height. Second accordion has heightStyle option set to fill, the script will automatically set the dimensions of the accordion to the height of its parent container.
$ (selector, context).accordion ("action", params) Method
The accordion ("action", params) method can perform an action on accordion elements, such as selecting/de-selecting the accordion menu. The action is specified as a string in the first argument (e.g., "disable" disables all menus). Check out the actions that can be passed, in the following table.
Syntax
$(selector, context).accordion ("action", params);;
The following table psts the different actions that can be used with this method −
Sr.No. | Action & Description |
---|---|
1 | This action destroys the accordion functionapty of an element completely. The elements return to their pre-init state. |
2 | This action disable all menus. No cpck will be taken into account. This method does not accept any arguments. |
3 | This action reactivate all menus. The cpcks are again considered. This method does not accept any arguments. |
4 | This action gets the value of currently associated accordion element with the specified optionName. This takes a String value as argument. |
5 | This action gets an object containing key/value pairs representing the current accordion options hash. |
6 | This action sets the value of the accordion option associated with the specified optionName. |
7 | This action sets one or more options for the accordion. |
8 | This action processes any headers and panels that were added or removed directly in the DOM. It then recomputes the height of the accordion panels. Results depend on the content and the heightStyle option. This method does not accept any arguments. |
9 | This action returns the accordion widget element; the one annotated with the ui-accordion class name. |
Example
Now let us see an example using the actions from the above table. The following example demonstrates the use of option( optionName, value ) method.
<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Accordion Example </title> <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <script> $(function() { $( "#accordion-5" ).accordion({ disabled: false }); $("input").each(function () { $(this).change(function () { if ($(this).attr("id") == "disableaccordion") { $("#accordion-5").accordion("option", "disabled", true); } else { $("#accordion-5").accordion("option", "disabled", false); } }); }); }); </script> <style> #accordion-5{font-size: 14px;} </style> </head> <body> <span id = "accordion-5"> <h3>Tab 1</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut apquip ex ea commodo consequat. </p> </span> <h3>Tab 2</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut apquip ex ea commodo consequat. </p> </span> <h3>Tab 3</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut apquip ex ea commodo consequat. </p> <ul> <p>List item one</p> <p>List item two</p> <p>List item three</p> </ul> </span> </span> <span style = "margin-top:30px"> <input type = "radio" name = "disable" id = "disableaccordion" value = "disable">Disable accordion <input type = "radio" name = "disable" id = "enableaccordion" checked value = "enable">Enable accordion </span> </body> </html>
Let us save the above code in an HTML file accordionexample.htm and open it in a standard browser which supports javascript, you must also see the following output −
Here we demonstrate enabpng and disabpng of the accordions. Select the respective radio buttons to check each action.
Event Management on accordion elements
In addition to the accordion (options) method which we saw in the previous sections, JqueryUI provides event methods which gets triggered for a particular event. These event methods are psted below −
Sr.No. | Event Method & Description |
---|---|
1 | This event is triggered when a menu is activated. This event is only fired on panel activation, it is not fired for the initial panel when the accordion widget is created. |
2 | This event is triggered before a panel is activated. This event can be canceled to prevent the panel from activating. |
3 | This event is triggered when the accordion is created. |
Example
The following example demonstrates the event method usage in accordion widgets. This example demonstrates the use of events create, beforeActive and active.
<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Accordion Example </title> <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <script> $(function() { $( "#accordion-6" ).accordion({ create: function (event, ui) { $("span#result").html ($("span#result").html () + "<b>Created</b><br>"); }, beforeActivate : function (event, ui) { $("span#result").html ($("span#result").html () + ", <b>beforeActivate</b><br>"); }, activate: function (event, ui) { $("span#result").html ($("span#result").html () + "<b>activate</b><br>"); } }); }); </script> <style> #accordion-6{font-size: 14px;} </style> </head> <body> <span id = "accordion-6"> <h3>Tab 1</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut apquip ex ea commodo consequat. </p> </span> <h3>Tab 2</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut apquip ex ea commodo consequat. </p> </span> <h3>Tab 3</h3> <span> <p> Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do eiusmod tempor incididunt ut labore et dolore magna apqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut apquip ex ea commodo consequat. </p> <ul> <p>List item one</p> <p>List item two</p> <p>List item three</p> </ul> </span> </span> <hr /> <span id = result></span> </body> </html>
Let us save the above code in an HTML file accordionexample.htm and open it in a standard browser which supports javascript, you must also see the following output −
Here we are displaying a text at the bottom, based on events.
Advertisements