jQuery 1.4.x and jQuery UI 1.8.x tabs

10:20 AM in Programming by Vic Russell

Tabs are simple to implement in jQuery – once you understand the basic structure of the tab element.

First, be sure to include the appropriate jquery libs – jquery base and the jquery ui.  Also, the jquery ui tabs lib must be included – this is usually done through jquery.ui.base (containing only @import directives [@import url("jquery.ui.tabs.css");]).

When using the jquery ui library for tabs, you must create tabs using an unordered list and list items containing <anchor html elements:

<ul>
    <li>
        <a href='#tabs-1'>This is tab 1</a>
    </li>
    <li>
        <a href='#tabs-2'>This is tab 2</a>
    </li>
</ul>
jQuery UI Tabs thus degrade gracefully to a list of links when Javascript is turned off in a client browser

You must also wrap the tabs in a container that is bound to an object reference and listener that implements the 'hidden' jquery ui tab classes on your custom tab.

Now, wrap the ul in a tab that will not close until the content panes are defined....

<div id='tabs'>
<ul>
    <li>
        <a href='#tabs-1'>This is tab 1</a>
    </li>
    <li>
        <a href='#tabs-2'>This is tab 2</a>
    </li>
</ul>
...
<!-- close the 'tabs' container>
</div>

Now that the <div id='tabs' and the referenced <anchor tabs are properly named, you can define the contents of each tab pane.

<div id='tabs-1'>
    This is the contents of tabs-1
</div>

<div id='tabs-2'>
    This is the contents of tabs-2
</div>

Now, place the contents WITHIN the < div id='tabs' > container...

<div id='tabs'>
<ul>
    <li>
        <a href='#tabs-1'>This is tab 1</a>
    </li>
    <li>
        <a href='#tabs-2'>This is tab 2</a>
    </li>
</ul>
<div id='tabs-1'>
    This is the contents of tabs-1
</div>

<div id='tabs-2'>
    This is the contents of tabs-2
</div>

</div>

Now, we need to associate the above construct with a jQuery initializer - linking the <tabs> with the jQuery UI code containing the binding code:

<script type='text/javascript'>
// listener for profileDemogForm
$(document).ready( function() {
$(function() {
    $("#profileTabMenu_id").tabs({
        collapsible: true
    });
});
</script>

For your test page, use the default jQ UI library (after you have downloaded or linked to it via Google ...)


<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

<link type='text/css' href="js/jquery_ui/jquery-ui-1.8.2/themes/base/jquery.ui.all.css" rel='stylesheet' />
<link type='text/css' href="js/jquery_ui/jquery-ui-1.8.2/themes/base/jquery.ui.core.css" rel='stylesheet' />
<link type='text/css' href="js/jquery_ui/jquery-ui-1.8.2/themes/base/jquery.ui.theme.css" rel='stylesheet' />
<link type='text/css' href="js/jquery_ui/jquery-ui-1.8.2/themes/base/jquery.ui.base.css" rel='stylesheet' /><<<

Some tips:

You can name a jquery tabs container anything - you just have to use that for the content pane tabs.

<div id='myTabs'>

Each bookmark reference would then follow the following semantic:

<a href="#myTabs-1">  and <a href="myTabs-2">

The -1 and -2 (dash 1 and dash 2) are very significant and must be followed for the jQ UI to pain these anchors as tabs.

content pane (aka: contentpane) the wrapper (named anything - I like contentpane or tabs_contentpane) that contains the container divs that further contain the html to display when the tab is clicked (in focus).

This content can be

  • inline html
  • an included page  using jQuery .load("url")
  • a pre-fetched server-side include (<%@ include file='tabcontent2.jsp %> or <? include("filename.php") ?>