Tabs are generally used to break content into multiple sections that can be swapped to save space, much like an accordion.
Tabs have a particular set of markup that must be used in order for them to work properly:
- The tabs themselves must be in either an ordered (
<ol>
) or unordered (<ul>
) list - Each tab "title" must be inside of a list item (
<li>
) and wrapped by an anchor (<a>
) with anhref
attribute - Each tab panel may be any valid element but it must have an id which corresponds to the hash in the anchor of the associated tab.
The content for each tab panel can be defined in-page or can be loaded via Ajax; both are handled automatically based on the href
of the anchor associated with the tab. By default tabs are activated on click, but the events can be changed to hover via the event
option.
Below is some sample markup:
<div id="tabs"> <ul> <li><a href="#fragment-1">One</a></li> <li><a href="#fragment-2">Two</a></li> <li><a href="#fragment-3">Three</a></li> </ul> <div id="fragment-1"> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. </div> <div id="fragment-2"> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. </div> <div id="fragment-3"> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. </div> </div>
Keyboard interaction
When focus is on a tab, the following key commands are available:
-
UP
/LEFT
: Move focus to the previous tab. If on first tab, moves focus to last tab. Activate focused tab after a short delay. -
DOWN
/RIGHT
: Move focus to the next tab. If on last tab, moves focus to first tab. Activate focused tab after a short delay. -
CTRL
+UP
/LEFT
: Move focus to the previous tab. If on first tab, moves focus to last tab. The focused tab must be manually activated. -
CTRL
+DOWN
/RIGHT
: Move focus to the next tab. If on last tab, moves focus to first tab. The focused tab must be manually activated. -
HOME
: Move focus to the first tab. Activate focused tab after a short delay. -
END
: Move focus to the last tab. Activate focused tab after a short delay. -
CTRL
+HOME
: Move focus to the first tab. The focused tab must be manually activated. -
CTRL
+END
: Move focus to the last tab. The focused tab must be manually activated. -
SPACE
: Activate panel associated with focused tab. -
ENTER
: Activate or toggle panel associated with focused tab. -
ALT
/OPTION
+PAGE UP
: Move focus to the previous tab and immediately activate. -
ALT
/OPTION
+PAGE DOWN
: Move focus to the next tab and immediately activate.
When focus is in a panel, the following key commands are available:
-
CTRL
+UP
: Move focus to associated tab. -
ALT
/OPTION
+PAGE UP
: Move focus to the previous tab and immediately activate. -
ALT
/OPTION
+PAGE DOWN
: Move focus to the next tab and immediately activate.
Theming
The tabs widget uses the jQuery UI CSS framework to style its look and feel. If tabs specific styling is needed, the following CSS class names can be used:
-
ui-tabs
: The outer container of the tabs. This element will additionally have a class ofui-tabs-collapsible
when thecollapsible
option is set.-
ui-tabs-nav
: The list of tabs.- The active list item in the nav will have a
ui-tabs-active
class. Any list item whose associated content is loading via an Ajax call will have aui-tabs-loading
class.-
ui-tabs-anchor
: The anchors used to switch panels.
-
- The active list item in the nav will have a
-
ui-tabs-panel
: The panels associated with the tabs. Only the panel whose corresponding tab is active will be visible.
-
Dependencies
- UI Core
- Widget Factory
- Effects Core (optional; for use with the
show
andhide
options)
- This widget requires some functional CSS, otherwise it won't work. If you build a custom theme, use the widget's specific CSS file as a starting point.
0
Multiple types supported:
- Boolean: Setting
active
tofalse
will collapse all panels. This requires thecollapsible
option to betrue
. - Integer: The zero-based index of the panel that is active (open). A negative value selects panels going backward from the last panel.
Code examples:
Initialize the tabs with the active
option specified:
$( ".selector" ).tabs({ active: 1 });
Get or set the active
option, after initialization:
// Getter var active = $( ".selector" ).tabs( "option", "active" ); // Setter $( ".selector" ).tabs( "option", "active", 1 );
false
true
, the active panel can be closed.Code examples:
Initialize the tabs with the collapsible
option specified:
$( ".selector" ).tabs({ collapsible: true });
Get or set the collapsible
option, after initialization:
// Getter var collapsible = $( ".selector" ).tabs( "option", "collapsible" ); // Setter $( ".selector" ).tabs( "option", "collapsible", true );
false
Multiple types supported:
- Boolean: Enable or disable all tabs.
- Array: An array containing the zero-based indexes of the tabs that should be disabled, e.g.,
[ 0, 2 ]
would disable the first and third tab.
Code examples:
Initialize the tabs with the disabled
option specified:
$( ".selector" ).tabs({ disabled: [ 0, 2 ] });
Get or set the disabled
option, after initialization:
// Getter var disabled = $( ".selector" ).tabs( "option", "disabled" ); // Setter $( ".selector" ).tabs( "option", "disabled", [ 0, 2 ] );
"click"
"mouseover"
.Code examples:
Initialize the tabs with the event
option specified:
$( ".selector" ).tabs({ event: "mouseover" });
Get or set the event
option, after initialization:
// Getter var event = $( ".selector" ).tabs( "option", "event" ); // Setter $( ".selector" ).tabs( "option", "event", "mouseover" );
"content"
-
"auto"
: All panels will be set to the height of the tallest panel. -
"fill"
: Expand to the available height based on the tabs' parent height. -
"content"
: Each panel will be only as tall as its content.
Code examples:
Initialize the tabs with the heightStyle
option specified:
$( ".selector" ).tabs({ heightStyle: "fill" });
Get or set the heightStyle
option, after initialization:
// Getter var heightStyle = $( ".selector" ).tabs( "option", "heightStyle" ); // Setter $( ".selector" ).tabs( "option", "heightStyle", "fill" );
null
Multiple types supported:
- Boolean: When set to
false
, no animation will be used and the panel will be hidden immediately. When set totrue
, the panel will fade out with the default duration and the default easing. - Number: The panel will fade out with the specified duration and the default easing.
- String: The panel will be hidden using the specified effect. The value can either be the name of a built-in jQuery animation method, such as
"slideUp"
, or the name of a jQuery UI effect, such as"fold"
. In either case the effect will be used with the default duration and the default easing. - Object: If the value is an object, then
effect
,delay
,duration
, andeasing
properties may be provided. If theeffect
property contains the name of a jQuery method, then that method will be used; otherwise it is assumed to be the name of a jQuery UI effect. When using a jQuery UI effect that supports additional settings, you may include those settings in the object and they will be passed to the effect. Ifduration
oreasing
is omitted, then the default values will be used. Ifeffect
is omitted, then"fadeOut"
will be used. Ifdelay
is omitted, then no delay is used.
Code examples:
Initialize the tabs with the hide
option specified:
$( ".selector" ).tabs({ hide: { effect: "explode", duration: 1000 } });
Get or set the hide
option, after initialization:
// Getter var hide = $( ".selector" ).tabs( "option", "hide" ); // Setter $( ".selector" ).tabs( "option", "hide", { effect: "explode", duration: 1000 } );
null
Multiple types supported:
- Boolean: When set to
false
, no animation will be used and the panel will be shown immediately. When set totrue
, the panel will fade in with the default duration and the default easing. - Number: The panel will fade in with the specified duration and the default easing.
- String: The panel will be shown using the specified effect. The value can either be the name of a built-in jQuery animation method, such as
"slideDown"
, or the name of a jQuery UI effect, such as"fold"
. In either case the effect will be used with the default duration and the default easing. - Object: If the value is an object, then
effect
,delay
,duration
, andeasing
properties may be provided. If theeffect
property contains the name of a jQuery method, then that method will be used; otherwise it is assumed to be the name of a jQuery UI effect. When using a jQuery UI effect that supports additional settings, you may include those settings in the object and they will be passed to the effect. Ifduration
oreasing
is omitted, then the default values will be used. Ifeffect
is omitted, then"fadeIn"
will be used. Ifdelay
is omitted, then no delay is used.
Code examples:
Initialize the tabs with the show
option specified:
$( ".selector" ).tabs({ show: { effect: "blind", duration: 800 } });
Get or set the show
option, after initialization:
// Getter var show = $( ".selector" ).tabs( "option", "show" ); // Setter $( ".selector" ).tabs( "option", "show", { effect: "blind", duration: 800 } );
- This method does not accept any arguments.
Invoke the destroy method:
$( ".selector" ).tabs( "destroy" );
- This signature does not accept any arguments.
Invoke the method:
$( ".selector" ).tabs( "disable" );
disabled
option: $( "#tabs" ).tabs( "option", "disabled", [ 1, 2, 3 ] )
. - indexType: NumberThe zero-based index of the tab to disable.
Invoke the method:
$( ".selector" ).tabs( "disable", 1 );
- hrefType: StringThe
href
of the tab to disable.
Invoke the method:
$( ".selector" ).tabs( "disable", "#foo" );
- This signature does not accept any arguments.
Invoke the method:
$( ".selector" ).tabs( "enable" );
$( "#example" ).tabs( "option", "disabled", [] );
.- indexType: NumberThe zero-based index of the tab to enable.
Invoke the method:
$( ".selector" ).tabs( "enable", 1 );
- hrefType: StringThe
href
of the tab to enable.
Invoke the method:
$( ".selector" ).tabs( "enable", "#foo" );
Retrieves the tabs's instance object. If the element does not have an associated instance, undefined
is returned.
Unlike other widget methods, instance()
is safe to call on any element after the tabs plugin has loaded.
- This method does not accept any arguments.
Invoke the instance method:
$( ".selector" ).tabs( "instance" );
- indexType: NumberThe zero-based index of the tab to load.
Invoke the method:
$( ".selector" ).tabs( "load", 1 );
- hrefType: StringThe
href
of the tab to load.
Invoke the method:
$( ".selector" ).tabs( "load", "#foo" );
Gets the value currently associated with the specified optionName
.
Note: For options that have objects as their value, you can get the value of a specific key by using dot notation. For example, "foo.bar"
would get the value of the bar
property on the foo
option.
- optionNameType: StringThe name of the option to get.
Invoke the method:
var isDisabled = $( ".selector" ).tabs( "option", "disabled" );
- This signature does not accept any arguments.
Invoke the method:
var options = $( ".selector" ).tabs( "option" );
Sets the value of the tabs option associated with the specified optionName
.
Note: For options that have objects as their value, you can set the value of just one property by using dot notation for optionName
. For example, "foo.bar"
would update only the bar
property of the foo
option.
- optionNameType: StringThe name of the option to set.
- valueType: ObjectA value to set for the option.
Invoke the method:
$( ".selector" ).tabs( "option", "disabled", true );
- optionsType: ObjectA map of option-value pairs to set.
Invoke the method:
$( ".selector" ).tabs( "option", { disabled: true } );
heightStyle
option.- This method does not accept any arguments.
Invoke the refresh method:
$( ".selector" ).tabs( "refresh" );
jQuery
object containing the tabs container. - This method does not accept any arguments.
Invoke the widget method:
var widget = $( ".selector" ).tabs( "widget" );
- This method does not accept any arguments.
Use the list with the class my-tabs
or fall back to the default implementation.
_getList: function() { var list = this.element.find( ".my-tabs" ); return list.length ? list.eq( 0 ) : this._super(); }
tabsactivate
Triggered after a tab has been activated (after animation completes). If the tabs were previously collapsed, ui.oldTab
and ui.oldPanel
will be empty jQuery objects. If the tabs are collapsing, ui.newTab
and ui.newPanel
will be empty jQuery objects.
activate
event is only fired on tab activation, it is not fired for the initial tab when the tabs widget is created. If you need a hook for widget creation use the create
event.Initialize the tabs with the activate callback specified:
$( ".selector" ).tabs({ activate: function( event, ui ) {} });
Bind an event listener to the tabsactivate event:
$( ".selector" ).on( "tabsactivate", function( event, ui ) {} );
tabsbeforeactivate
ui.oldTab
and ui.oldPanel
will be empty jQuery objects. If the tabs are collapsing, ui.newTab
and ui.newPanel
will be empty jQuery objects.Initialize the tabs with the beforeActivate callback specified:
$( ".selector" ).tabs({ beforeActivate: function( event, ui ) {} });
Bind an event listener to the tabsbeforeactivate event:
$( ".selector" ).on( "tabsbeforeactivate", function( event, ui ) {} );
tabsbeforeload
Triggered when a remote tab is about to be loaded, after the beforeActivate
event. Can be canceled to prevent the tab panel from loading content; though the panel will still be activated. This event is triggered just before the Ajax request is made, so modifications can be made to ui.jqXHR
and ui.ajaxSettings
.
Note: Although ui.ajaxSettings
is provided and can be modified, some of these properties have already been processed by jQuery. For example, prefilters have been applied, data
has been processed, and type
has been determined. The beforeLoad
event occurs at the same time, and therefore has the same restrictions, as the beforeSend
callback from jQuery.ajax()
.
- eventType: Event
- uiType: Object
- tabType: jQueryThe tab that is being loaded.
- panelType: jQueryThe panel which will be populated by the Ajax response.
- jqXHRType: jqXHRThe
jqXHR
object that is requesting the content. - ajaxSettingsType: ObjectThe properties that will be used by
jQuery.ajax
to request the content.
-
Initialize the tabs with the beforeLoad callback specified:
$( ".selector" ).tabs({ beforeLoad: function( event, ui ) {} });
Bind an event listener to the tabsbeforeload event:
$( ".selector" ).on( "tabsbeforeload", function( event, ui ) {} );
tabscreate
ui.tab
and ui.panel
will be empty jQuery objects.Initialize the tabs with the create callback specified:
$( ".selector" ).tabs({ create: function( event, ui ) {} });
Bind an event listener to the tabscreate event:
$( ".selector" ).on( "tabscreate", function( event, ui ) {} );
tabsload
Initialize the tabs with the load callback specified:
$( ".selector" ).tabs({ load: function( event, ui ) {} });
Bind an event listener to the tabsload event:
$( ".selector" ).on( "tabsload", function( event, ui ) {} );
A simple jQuery UI Tabs
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>tabs demo</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> </head> <body> <div id="tabs"> <ul> <li><a href="#fragment-1"><span>One</span></a></li> <li><a href="#fragment-2"><span>Two</span></a></li> <li><a href="#fragment-3"><span>Three</span></a></li> </ul> <div id="fragment-1"> <p>First tab is active by default:</p> <pre><code>$( "#tabs" ).tabs(); </code></pre> </div> <div id="fragment-2"> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. </div> <div id="fragment-3"> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. </div> </div> <script> $( "#tabs" ).tabs(); </script> </body> </html>
Please login to continue.