Last Update: 22-Jul-2011
Today when I was building a web application interface, I was trying to use jQuery UI Accordion, but I didn’t want it to open one tab at a time, I just wanted each accordion tab to toggle between open and close states while pressing it, so I can have more than one tab opened, what I wanted was not a feature of jQuery UI’s Accordion, so I tried to look up over the internet if I can find any suitable solution, but could not find anything, so I decided to make the plugin myself,
it works the same way original jQuery UI Accordion do, below you can find the markup and script required to run this plugin,also you can download a working example along with the plugin from my google code profile
Markup
<div id="multiAccordion"> | |
<h3><a href="#">tab 1</a></h3> | |
<div> | |
Lorem ipsum dolor sit amet | |
</div> | |
<h3><a href="#">tab 2</a></h3> | |
<div> | |
Lorem ipsum dolor sit amet | |
</div> | |
<h3><a href="#">tab 3</a></h3> | |
<div> | |
Lorem ipsum dolor sit amet | |
</div> | |
<h3><a href="#">tab 4</a></h3> | |
<div> | |
Lorem ipsum dolor sit amet | |
</div> | |
</div> |
Script
$(function(){ | |
$('#multiAccordion').multiAccordion(); | |
}); |
UPDATE:
$(function(){ | |
// this will make the second tab by default opened (index starts from 0) | |
$('#multiAccordion').multiAccordion({active: 1 }); | |
// [ OR ] | |
// supports multiple tabs to be opened by default | |
$('#multiAccordion').multiAccordion({active: [1, 2, 3] }); | |
// show all tabs | |
$('#multiAccordion').multiAccordion({active: 'all' }); | |
// hide all tabs | |
$('#multiAccordion').multiAccordion({active: 'none' }); | |
// you can set the options as any jQuery UI plugin using option method | |
$('#multiAccordion').multiAccordion('option', 'active', 'all'); | |
}); |
you have the option to choose which tabs by default will be opened by passing either a number
or array
of desired open tabs to active option as follows:
UPDATE:
Events
/* fired when any tab is clicked, ui here hold clicked tab and its content | |
* ui['tab'] // current tab | |
* ui['content'] // current tab content (div) | |
*/ | |
click: function(event, ui) {} | |
// when accordion is initialized, ui here holds a jQuery object to the whole accordion | |
init: function(event, ui) {} | |
// when tab is shown, ui here hold the same as in click event above | |
tabShown: function(event, ui) {} | |
// when tab is hidden, ui here hold the same as in click event above | |
tabHidden: function(event, ui) {} | |
// How can you bind these events ? | |
// 1. at plugin initialization | |
$('#multiAccordion').multiAccordion({ | |
active: 'all', | |
click: function(event, ui) { | |
alert('tab is clicked!'); | |
} | |
}); | |
// 2. after initialization | |
$('#multiAccordion').multiAccordion('multiAccordionclick', function(){ | |
alert('also clicked'); | |
}); |
Methods
// destroying the plugin and removing all handlers, and styles | |
$('#multiAccordion').multiAccordion('destroy'); | |
// get an array of active tabs, if no active tabs it will return a one element array with [-1] value | |
$('#multiAccordion').multiAccordion('getActiveTabs'); | |
// you can use the previous method to get active tabs value, then you can set it later simply by | |
// retrieve | |
var activeTabs = $('#multiAccordion').multiAccordion('getActiveTabs'); | |
// set | |
$('#multiAccordion').multiAccordion('option', 'active', activeTabs); |
v.1.5: change log
- using jQuery UI widget framework
- fixed: browser goes to the top on tab click
- fixed: events binding repeated when calling plugin more than once
- enhancement: adding click, tabShown, tabHidden, and init event bindings
- enhancement: adding show all & hide all tabs
you can download the source file and an example from google code here
also you are welcome to make any contribution 🙂
follow me on:
awesome icons by WPZOOM