Dave Bush has been programming for 20 years. He currently blogs about .NET at http://blog.dmbcllc.com and provides .NET coaching to small and medium sized companies. Dave is a DZone MVB and is not an employee of DZone and has posted 36 posts at DZone. You can read more from them at their website. View Full User Profile

jQuery Tabs

04.09.2009
| 8948 views |
  • submit to reddit
While the TabControl provided by Microsoft in the AJAX toolkit is probably a bit easier to use, the Tabs in jQuery are not much harder to implement and give us a bit more flexibility.

Here are the steps to implement:

First, make sure the HTML (ASPX) page you are creating is pointing at the stylesheet that contains the jQuery UI theme you are using. The tabs make use of the themes and will not render as tabs unless the styles are pulled in.

Next you’ll need to create the HTML. Everything that has to do with tabs should be wrapped in a DIV. You can either give the ID a class or give it an ID, as long as you can select it later to apply the tabs() method that turns it all into a tabbed interface.

Within that DIV, you’ll place an unordered list:

<ul>
<li><a href="#firstTab">First</a></li>
<li><a href="#secondTab">Second</a></li>
<li><a href="#thirdTab">Third</a></li>
<li><a href="#forthTab">Forth</a></li>
</ul>

which represents your tabs. Notice the href=”#id” stuff. Those are pointing to ids of the DIVS that come next.

<div id="firstTab">first content</div>
<div id="secondTab">second content</div>
<div id="thirdTab">Third content</div>
<div id="forthTab">Forth content</div>

Next, in your jQuery code, select the outer DIV and call tabs()

$(function() {
$("#tabs").tabs();
});

There are a ton of options for the tab control that would take several post to cover, but I do want to point out just a few.

First, you can add tabs on the fly and remove them on the fly using the add() and remove() methods.

Second, you can load the tab’s content on the fly by using a real URL in the href, specifying a title tag and creating a similar ID for the DIV that represents the content area (spaces are replaced with underscores). If you do this you can also pass in an option that tells it to cache the retrieved content or go after it every time the tab is requested.

<\/scr"+"ipt>"); //]]>--><a href="http://blog.dmbcllc.com/openx/www/delivery/ck.php?n=a72fb16b&amp;cb=INSERT_RANDOM_NUMBER_HERE' target='_blank'><img src="http://blog.dmbcllc.com/openx/www/delivery/avw.php?zoneid=2&amp;n=a72fb16b' border='0" alt='' /></a>

Finally, you can easily enable and disable tabs by calling tabs() again with tabs(‘enable’,tabIndex) or tabs(‘disable’,tabIndex)

For the full documentation, see http://jqueryui.com/demos/tabs/

 

References
Published at DZone with permission of Dave Bush, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Theo Shu replied on Wed, 2009/04/15 - 9:12am

Dave,

You're performing a valuable service to the community. I tried to implement the jQuery UI tabs a few weeks ago and finally just gave up. While I like the concept of loading the tabs on the fly, it created way too many headaches with masterpage assignments. I could never get the spinner to work and finally just gave up on it as the documentation was just horrible/incomplete/wrong. 

Please keep up the good work on this subject. We all could use the help! 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.