Home  >  

Playing with jQuery Tabs

Author photo
AddThis Social Bookmark Button
Before I begin, a quick apology to InsideRIA readers. I was asked to join the team last year, and wrote for the blog for a short time, but then, life, also known as a new job, hit me hard and I simply didn't have the time to write. I'm pleased to be able to return, and want to thank OReilly for having me back. Since my last visit, I've had a lot more exposure to Flex (via some kick butt development at work) and have recently spent a lot of time with jQuery. I hope to spend the new few weeks speaking about jQuery and integration with ColdFusion in particular. My plan is to have a regular blog entry on jQuery early in the week. (Normally about me something learning new about jQuery!) Later in the week I plan on rounding up links to blog entries, forums, new/updated plugins, that may be of interest to readers. I encourage folks to send me links during the week (ray at camdenfamily dot com). Ok, enough preamble. Let's talk about tabs. I've recently began playing with the jQuery UI library and am finding it quite useful. I had a bit of trouble at first (see my fumbling attempts here), but now that I've gotten familiar with the "jQuery UI way" I feel a lot more comfortable. Tonight I spent some time with the Tabs component. Like all jQuery UI components, you can head over to the Build Your Download to create a customized zip. I got by with the bare minimum of Core and Tabs with the default theme. Creating tabs is as simple as setting up some simple HTML and "enabling" it with a line of JavaScript code. Here is a bare bones example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<link type="text/css" rel="stylesheet" href="theme/ui.all.css" />
<script src="jquery-1.3.1.js"></script>
<script src="jquery-ui-personalized-1.6rc6.js"></script>
<script>
$(document).ready(function() {
 	$("#example").tabs();					   
});
</script>
</head>

<body>

<div id="example">
     <ul>
         <li><a href="#first-tab"><span>Content 1</span></a></li>
         <li><a href="#second-tab"><span>Content 2</span></a></li>
         <li><a href="#third-tab"><span>Content 3</span></a></li>
     </ul>
	 
	 <div id="first-tab">
	 This is the first tab.
	 </div>

	 <div id="second-tab">
	 This is the second tab.
	 </div>

	 <div id="third-tab">
	 This is the third tab.
	 </div>

</div>

</body>
</html>
If you ignore the JavaScript, all we have here is a simple unordered list and 3 div items. The JavaScript we do have: $("#example").tabs() simply re-designed the list into tabs. I love how simple that works and even better - it is immediately evident that even without JavaScript enabled this is still going to be a page a person can read. (You can see this demo here). Modifying the code to load content via Ajax is even simpler. Changing the link from an internal anchor to an external page url handles everything:

<div id="example">
     <ul>
         <li><a href="#first-tab"><span>Content 1</span></a></li>
         <li><a href="second.html"><span>Content 2</span></a></li>
         <li><a href="#third-tab"><span>Content 3</span></a></li>
     </ul>
	 
	 <div id="first-tab">
	 This is the first tab.
	 </div>

	 <div id="third-tab">
	 This is the third tab.
	 </div>

</div>
Notice I've modified the second LI item to point to second.html. I've also removed the div since the content is no longer on the page. To the user though this just plain works and once again it is about as simple as things can get. You can see the complete demo here. So the docs go into much greater detail about what the tabs support. As you can probably guess there are various event handlers and methods as well as numerous options on how the tabs are set up. I thought it would be interesting to see how difficult it was to create tabs that could be bookmarked. The docs mention a cookie option which requires an optional download of another library, but the cookie option would allow let one user to return to his or her previous tab. It wouldn't let me share a URL to a specific tab with someone else. I began by adding some code to handle the tab select event:
$("#example").bind('tabsselect', function(event, ui) {
 	document.location='#'+(ui.index+1);
});
The tabsselect event runs when a tab is selected. One of the values I have access to is the tab's selected index. This is 0 based, but I was worried that would confuse users so I added one to it. I change the browser's URL to point to #N where N will be the tab selected. Simple enough to set, but how do we handle noticing it on the page load?
if(document.location.hash!='') {
       //get the index
       indexToLoad = document.location.hash.substr(1,document.location.hash.length);
 	  $("#example").tabs('select',indexToLoad-1);
}
This code checks the browser for a hash value. If one exists, we use the tabs method API to set the value. Since I used a 1 based index for my URLs (sorry, you will never convince me 0 based indexes make sense) I subtract one from the value before passing it to the API. You can see an example of this here: http://www.coldfusionjedi.com/demos/jquerytabs/test3.html#2. The #2 in the URL should auto-select the second tab. Ok, one more example, this time with ColdFusion. Here is a quick example where every tab links to one particular dynamic file:
<div id="example">
     <ul>
         <li><a href="content.cfm?page=1"><span>Content 1</span></a></li>
         <li><a href="content.cfm?page=2"><span>Content 2</span></a></li>
         <li><a href="content.cfm?page=3"><span>Content 3</span></a></li>
     </ul>
</div>
My content.cfm file is rather simple. A more real world example would hit the database, a CFC, or whatever:
<cfparam name="url.page" default="1">

<!--- something dynamic here... --->
<cfoutput>
<h1>Page #url.page#</h1>

<p>
This is dynamic content about page #url.page#.
</p>
</cfoutput>
It isn't too exciting but you can see this here. Any comments? How about real life examples? Anyone using this on their site?

Read more from Raymond Camden. Raymond Camden's Atom feed cfjedimaster on Twitter

Comments

23 Comments

Sam said:

Very Nice!

Good starting blocks for a Tab switcher using JQuery

I use a Style of what ray is using at "http://www.fusion-reactor.com/" But I have a little added feature that allows it to work when JavaScript is turned off!

If anyone would like to know how its done twitter me @sam_george

I don't suppose I could get you to share here, help foster the conversation? :)

Ray, you just saved me a world of pain. I'd been trying to figure out how to use Tabs without the Tabs. Guess I should have just RTFM, but

$("#example").tabs('select',indexToLoad-1);

was all I needed, plus hiding the UL that defines the tabs. Now I can have a multi-page form and control moving from tab to tab based on the validation of the current tab's form fields.

Thanks.

Gercek said:

How can we setup animation on tabs? Similar to the earlier version of the tabs.

http://www.stilbuero.de/jquery/tabs/#fragment-15

Hmm, I'm not seeing anything obvious in the docs. I'd be willing to bet it IS possible.

David Betz said:

Nice, Ray, thanks. This got my brain working. You can also make the hash more informative by using:

document.location='#'+(ui.panel.id);

instead of:

document.location='#'+(ui.index);

That's a good tip there. It will also work if you re-order, add, tabs.

Thanks for share Ray.

Guys, I have a problem, I do process(process.php) a tab with ajaxsubmit, so I disable the first tab and enable the 2nd with success. ok till here, but the php session from the former announce doesnt update even unsetting session variables on process.php. It looks like I need refresh the page to get updated(process.php) session variables values, but (refresh) isnt in mind.

If someone knows the solution please e-mail me (albertomarlboro@rocketmail.com)

Vivek said:

Hi,

In the dynamic page, I have a submit button also. How do I save the data into the database, without being redirected.

Submit/Postback within JQuery UI Tabs causing redirect

James said:

Nice article.

I have tabs created using JQuery. Now on clicking on a tab, I want to open a link, lets say "www.yahoo.com" inside the tab. yahoo.com will be my tab content. is that possible and if so how to do it? Also on clicking another tab, I want to open a jsp file in my project is that possible? how? Thanks in advance.

As far as I know, you can only do it using server side code. The tab would load a URL on your system and you would use code to download the HTML of the remote URL.

Jason said:

Awesome exactly what I was looking for - they should have this as part of the doco.

Thanks. Most of my jQuery posts are simply my way of expressing how I learned a feature.

Jeff said:

How do you enable more than five tabs

I'm not aware of any limit to 5. What happened when you tried 6?

Taylor Hunt said:

The example of bookmark-able tabs was EXACTLY what I've spent countless hours trying to get to function. I was having issues with forms being posted and users losing what tab-space they were on. If you're ever in Wichita, Ks - I'm buying you a beer. Thanks man!

Warning - I'm a beer snob. It could be a pricey import. :)

Taza Guru said:

Thanks - this helped big time. I am not sure why the tab selected does not work on http://dev.ntroduction.com/?page=favors&cat=jobs Appreciate your help!

No idea - have you done any debugging?

Pete Jones said:

I second the nomination to include this in the jQueryUI docs - has anybody actually formally requested this?

I nearly switched out some jQueryUI tabs in favor of the jQuery Tools tabs simply because jQuery Tools supports linking right into tabs out of the box. Especially in the web app world, where interlinking is so heavy and "pages" are a concept that barely contains all you cram into a single page, this feature is critical to tabs.

Maxim said:

Hi there, thanks for the code but how would you assign url for nested tabs like in this example

http://stilbuero.de/jquery/tabs_3/nested.html
thanks if u have time to answer

Leave a comment


Tag Cloud

Question of the Week: Open Source Flex Projects

What would you say are the 5 most prominent open source projects in the Flex world?

Answer

Latest Features

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.