jQuery: The Write Less, Do More JavaScript Library

UI/Tabs

From jQuery JavaScript Library

Jump to: navigation, search

« Back to the jQuery UI Docs

Organize your content into navigable tabs.

Contents

Depends on

  • renamed "ui.core.js" from "ui.base.js"
  • Optional, for using the cookie option: jquery.cookie.js

Example

NameType
Example:
A simple jQuery UI Tabbed pane.

$("#example > ul").tabs();

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <link rel="stylesheet" href="http://dev.jquery.com/view/trunk/themes/flora/flora.all.css" type="text/css" media="screen" title="Flora (Default)">
  <script type="text/javascript" src="http://dev.jquery.com/view/trunk/ui/ui.core.js"></script>
  <script type="text/javascript" src="http://dev.jquery.com/view/trunk/ui/ui.tabs.js"></script>


  <script>
  $(document).ready(function(){
    $("#example > ul").tabs();
  });
  </script>
  
</head>
<body>
  
        <div id="example" class="flora">
            <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>$('#example > ul').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>
</body>
</html>

NameType

Documentation

NameType
Plugin methods:











NameType
tabs( options ) Returns: jQuery
The method that you use to create a new tabs interface.
tabs( "add", url, label, index ) Returns: jQuery
Add a new tab.
tabs( "remove", index ) Returns: jQuery
Remove a tab.
tabs( "enable", index ) Returns: jQuery
Enable a disabled tab.
tabs( "disable", index ) Returns: jQuery
Disable a tab.
tabs( "select", index ) Returns: jQuery
Select a tab, i.e., simulate a click programmatically.
tabs( "load", index ) Returns: jQuery
Reload the content of an Ajax tab programmatically.
tabs( "url", index, url ) Returns: jQuery
Change the url from which an Ajax (remote) tab will be loaded.
tabs( "destroy" ) Returns: jQuery
Destroy tabs interface and revert HTML to the state before creating tabs.
tabs( "length" ) Returns: Number
Retrieve the number of tabs of the first matched tab pane.
tabs( "rotate", ms, continuing ) Returns: jQuery
Set up an automatic rotation through tabs of a tab pane.

Events

Certain events are fired when interacting with a tabs interface: tabsselect, tabsload, tabsshow (in that order), tabsadd, tabsremove, tabsenable, tabsdisable.

Example:

$('.ui-tabs-nav').bind('tabsselect', function(event, ui) {
    ui.instance // internal widget instance
    ui.options // options used to intialize this widget
    ui.tab // anchor element of the currently shown tab
    ui.panel // element, that contains the contents of the currently shown tab
});

Note that if a handler for the tabsselect event returns false, the clicked tab will not be selected (useful for example if switching to the next tab requires a form validation).

Ajax mode

Tabs supports loading tab content via Ajax in an unobtrusive manner.

The HTML you need is slightly different from the one that is used for static tabs: A list of links pointing to existing resources (from where the content gets loaded) and no additional containers at all (unobtrusive!). The containers' markup is going to be created on the fly:

<div id="container">
     <ul>
         <li><a href="ahah_1.html">Content 1</a></li>
         <li><a href="ahah_2.html">Content 2</a></li>
         <li><a href="ahah_3.html">Content 3</a></li>
     </ul>
</div>

Obviously this degrades gracefully - the links, e.g. the content, will still be accessible with JavaScript disabled.

Note that if you wish to reuse an existing container, you could do so by matching a title attribute and the container's id:

<li><a href="hello/world.html" title="Todo Overview"> ... </a></li>

and a container like:

<div id="Todo_Overview"> ... </div>

(Note how white space is replaced with an underscore)

This is useful if you want a human readable hash in the URL instead of a cryptic generated one.

FAQ

Does UI Tabs support back button and bookmarking of tabs?

Tabs 2 already supported that functionality, although the history plugin needs a rewrite first (it doesn't support Safari 3 and is in general a little inflexible) before it can be build back into the tabs. It is planned and Klaus is working on it whenever he finds the time. Actual bugs in the UI Tabs plugin itself always have higher priority though.

How can I...

...retrieve the currently selected tab?

var $tabs = $('#example').tabs();
var selected = $tabs.data('selected.tabs');

...prevent switching to the tab on click depending on form validation?

Returning false in the tabs select handler prevents the clicked tab from becoming selected.

$('#example').tabs({
    select: function(ui) {
        var isValid = ... // form validation returning true or false
        return isValid;
    }
});

...immediately select a just added tab?

var $tabs = $('#example').tabs({
    add: function(ui) {
        $tabs.tabs('select', '#' + ui.panel.id);
    }
});

...open a tab in a new window instead?

Note that opening a tab in a new window is unexpected, e.g. inconsistent behaviour exposing a serious usablity problem (http://www.useit.com/alertbox/tabs.html).

$('#example').tabs({
    select: function(ui) {
        location.href = $.data(ui.tab, 'load.tabs');
        return false;
    }
});

Demos

Theming