Tabs Widget | jQuery UI API Documentation (original) (raw)

Description: A single content area with multiple panels, each associated with a header in a list.

QuickNavExamples

Options

Methods

Extension Points

Events

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 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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <li><a href="#fragment-1">One</a></li> <li><a href="#fragment-2">Two</a></li> <li><a href="#fragment-3">Three</a></li> 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.

Keyboard interaction

When focus is on a tab, the following key commands are available:

When focus is in a panel, the following key commands are available:

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 for overrides or as keys for the classes option:

Dependencies

Additional Notes:

Options

active

Default: 0

Which panel is currently open.

Multiple types supported:

Code examples:

Initialize the tabs with the active option specified:

Get or set the active option, after initialization:

1 2 3 4 5 var active = (".selector").tabs("option","active");( ".selector" ).tabs( "option", "active" );(".selector").tabs("option","active");( ".selector" ).tabs( "option", "active", 1 );

classes

Default:

"ui-tabs": "ui-corner-all",

"ui-tabs-nav": "ui-corner-all",

"ui-tabs-tab": "ui-corner-top",

"ui-tabs-panel": "ui-corner-bottom"

Specify additional classes to add to the widget's elements. Any of classes specified in the Theming section can be used as keys to override their value. To learn more about this option, check out the learn article about the classes option.

Code examples:

Initialize the tabs with the classes option specified, changing the theming for the ui-tabs class:

Get or set a property of the classes option, after initialization, here reading and changing the theming for the ui-tabs class:

1 2 3 4 5 var themeClass = (".selector").tabs("option","classes.ui−tabs");( ".selector" ).tabs( "option", "classes.ui-tabs" );(".selector").tabs("option","classes.uitabs");( ".selector" ).tabs( "option", "classes.ui-tabs", "highlight" );

collapsible

Default: false

When set to true, the active panel can be closed.

Code examples:

Initialize the tabs with the collapsible option specified:

Get or set the collapsible option, after initialization:

1 2 3 4 5 var collapsible = (".selector").tabs("option","collapsible");( ".selector" ).tabs( "option", "collapsible" );(".selector").tabs("option","collapsible");( ".selector" ).tabs( "option", "collapsible", true );

disabled

Default: false

Which tabs are disabled.

Multiple types supported:

Code examples:

Initialize the tabs with the disabled option specified:

Get or set the disabled option, after initialization:

1 2 3 4 5 var disabled = (".selector").tabs("option","disabled");( ".selector" ).tabs( "option", "disabled" );(".selector").tabs("option","disabled");( ".selector" ).tabs( "option", "disabled", [ 0, 2 ] );

event

Default: "click"

The type of event that the tabs should react to in order to activate the tab. To activate on hover, use "mouseover".

Code examples:

Initialize the tabs with the event option specified:

Get or set the event option, after initialization:

1 2 3 4 5 var event = (".selector").tabs("option","event");( ".selector" ).tabs( "option", "event" );(".selector").tabs("option","event");( ".selector" ).tabs( "option", "event", "mouseover" );

heightStyle

Default: "content"

Controls the height of the tabs widget and each panel. Possible values:

Code examples:

Initialize the tabs with the heightStyle option specified:

Get or set the heightStyle option, after initialization:

1 2 3 4 5 var heightStyle = (".selector").tabs("option","heightStyle");( ".selector" ).tabs( "option", "heightStyle" );(".selector").tabs("option","heightStyle");( ".selector" ).tabs( "option", "heightStyle", "fill" );

hide

Default: null

If and how to animate the hiding of the panel.

Multiple types supported:

Code examples:

Initialize the tabs with the hide option specified:

1 2 3 hide: { effect: "explode", duration: 1000 }

Get or set the hide option, after initialization:

1 2 3 4 5 var hide = (".selector").tabs("option","hide");( ".selector" ).tabs( "option", "hide" );(".selector").tabs("option","hide");( ".selector" ).tabs( "option", "hide", { effect: "explode", duration: 1000 } );

show

Default: null

If and how to animate the showing of the panel.

Multiple types supported:

Code examples:

Initialize the tabs with the show option specified:

1 2 3 show: { effect: "blind", duration: 800 }

Get or set the show option, after initialization:

1 2 3 4 5 var show = (".selector").tabs("option","show");( ".selector" ).tabs( "option", "show" );(".selector").tabs("option","show");( ".selector" ).tabs( "option", "show", { effect: "blind", duration: 800 } );

Methods

destroy()Returns: jQuery (plugin only)

Removes the tabs functionality completely. This will return the element back to its pre-init state.

Code examples:

Invoke the destroy method:

1 $( ".selector" ).tabs( "destroy" );

disable()Returns: jQuery (plugin only)

Disables all tabs.

Code examples:

Invoke the method:

1 $( ".selector" ).tabs( "disable" );

disable( index )Returns: jQuery (plugin only)

Disables a tab. The selected tab cannot be disabled. To disable more than one tab at once, set the disabled option: $( "#tabs" ).tabs( "option", "disabled", [ 1, 2, 3 ] ).

Code examples:

Invoke the method:

1 $( ".selector" ).tabs( "disable", 1 );

disable( href )Returns: jQuery (plugin only)

Disables a tab. The selected tab cannot be disabled.

Code examples:

Invoke the method:

1 $( ".selector" ).tabs( "disable", "#foo" );

enable()Returns: jQuery (plugin only)

Enables all tabs.

Code examples:

Invoke the method:

1 $( ".selector" ).tabs( "enable" );

enable( index )Returns: jQuery (plugin only)

Enables a tab. To enable more than one tab at once reset the disabled property like: $( "#example" ).tabs( "option", "disabled", [] );.

Code examples:

Invoke the method:

1 $( ".selector" ).tabs( "enable", 1 );

enable( href )Returns: jQuery (plugin only)

Enables a tab.

Code examples:

Invoke the method:

1 $( ".selector" ).tabs( "enable", "#foo" );

instance()Returns: Object

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.

Code examples:

Invoke the instance method:

1 $( ".selector" ).tabs( "instance" );

load( index )Returns: jQuery (plugin only)

Loads the panel content of a remote tab.

Code examples:

Invoke the method:

1 $( ".selector" ).tabs( "load", 1 );

load( href )Returns: jQuery (plugin only)

Loads the panel content of a remote tab.

Code examples:

Invoke the method:

1 $( ".selector" ).tabs( "load", "#foo" );

option( optionName )Returns: Object

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.

Code examples:

Invoke the method:

1 var isDisabled = $( ".selector" ).tabs( "option", "disabled" );

option()Returns: PlainObject

Gets an object containing key/value pairs representing the current tabs options hash.

Code examples:

Invoke the method:

1 var options = $( ".selector" ).tabs( "option" );

option( optionName, value )Returns: jQuery (plugin only)

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.

Code examples:

Invoke the method:

1 $( ".selector" ).tabs( "option", "disabled", true );

option( options )Returns: jQuery (plugin only)

Sets one or more options for the tabs.

Code examples:

Invoke the method:

1 $( ".selector" ).tabs( "option", { disabled: true } );

refresh()Returns: jQuery (plugin only)

Process any tabs that were added or removed directly in the DOM and recompute the height of the tab panels. Results depend on the content and the heightStyle option.

Code examples:

Invoke the refresh method:

1 $( ".selector" ).tabs( "refresh" );

Extension Points

The tabs widget is built with thewidget factory and can be extended. When extending widgets, you have the ability to override or add to the behavior of existing methods. The following methods are provided as extension points with the same API stability as the plugin methods listed above. For more information on widget extensions, seeExtending Widgets with the Widget Factory.


_getList()Returns: jQuery

Determine which list should be converted to tabs. By default the first descendant list is used.

Code examples:

Use the list with the class my-tabs or fall back to the default implementation.

1 2 3 4 var list = this.element.find( ".my-tabs" ); return list.length ? list.eq( 0 ) : this._super();

Events

activate( event, ui )Type: 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.

Note: Since the 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.

Code examples:

Initialize the tabs with the activate callback specified:

1 2 3 activate: function( event, ui ) {}

Bind an event listener to the tabsactivate event:

1 $( ".selector" ).on( "tabsactivate", function( event, ui ) {} );

beforeActivate( event, ui )Type: tabsbeforeactivate

Triggered immediately before a tab is activated. Can be canceled to prevent the tab from activating. If the tabs are currently 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.

Code examples:

Initialize the tabs with the beforeActivate callback specified:

1 2 3 beforeActivate: function( event, ui ) {}

Bind an event listener to the tabsbeforeactivate event:

1 $( ".selector" ).on( "tabsbeforeactivate", function( event, ui ) {} );

beforeLoad( event, ui )Type: 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().

Code examples:

Initialize the tabs with the beforeLoad callback specified:

1 2 3 beforeLoad: function( event, ui ) {}

Bind an event listener to the tabsbeforeload event:

1 $( ".selector" ).on( "tabsbeforeload", function( event, ui ) {} );

create( event, ui )Type: tabscreate

Triggered when the tabs are created. If the tabs are collapsed, ui.tab and ui.panel will be empty jQuery objects.

Code examples:

Initialize the tabs with the create callback specified:

1 2 3 create: function( event, ui ) {}

Bind an event listener to the tabscreate event:

1 $( ".selector" ).on( "tabscreate", function( event, ui ) {} );

load( event, ui )Type: tabsload

Triggered after a remote tab has been loaded.

Code examples:

Initialize the tabs with the load callback specified:

1 2 3 load: function( event, ui ) {}

Bind an event listener to the tabsload event:

1 $( ".selector" ).on( "tabsload", function( event, ui ) {} );

Example:

A simple jQuery UI Tabs

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 <link rel="stylesheet" href="" title="undefined" rel="noopener noreferrer">https://code.jquery.com/ui/1.13.3/themes/smoothness/jquery-ui.css"\> <script src="" title="undefined" rel="noopener noreferrer">https://code.jquery.com/jquery-3.7.1.js"\></script> <script src="" title="undefined" rel="noopener noreferrer">https://code.jquery.com/ui/1.13.3/jquery-ui.js"\></script> <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> <p>First tab is active by default:</p> <pre><code>$( "#tabs" ).tabs(); </code></pre> 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. 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.

Demo: