Accessible, unobtrusive JavaScript tabs with jQuery
May 13th, 2006
- Category:
- JavaScript,
- Web Development,
- jQuery
Here is a jQuery plugin that lets you create JavaScript tabs very easily – once you assembled the HTML with just one line of JavaScript code.
Starting with HTML the plugin relies on the following structure:
<div id="container">
<ul>
<li><a href="#section-1">Section 1</a></li>
<li><a href="#section-2">Section 2</a></li>
<li><a href="#section-3">Section 3</a></li>
</ul>
<div id="section-1">
...
</div>
<div id="section-2">
...
</div>
<div id="section-3">
...
</div>
</div>
There is a div with an id (named container here) that holds an unordered list with anchors on top and some divs following. Each anchor in the list points directly to a section below represented by one of these divs (more technically spoken: the URI in the href attribute refers to the fragment with the corresponding id attribute).
This is quite common – think of a TOC – and works fine already without JavaScript. So clicking on one of the links will take you immediatly to the appropriate section. And thats why it is accessible and unobtrusive. The HTML itself is all natural, no need for extraneous elements added only for scripting purposes.
This is nearly it. To create the JavaScript enhanced tabs you call the function $.tabs with one or an optional second argument.
The first argument has to to be the id of the div containing the list and the sections each of which will become the content of the corresponding tab:
$.tabs("container"); // first tab on by default
That way the first tab is on by default. You can specify which tab has to be on first with the second argument (no zero-based index):
$.tabs("container", 2); // second tab on
I recommend to attach this initialization of the tabs to jQuery’s ready event, which is not exactly the same as the load event, in an external file:
$(document).ready(function() {
$.tabs("container");
});
Here is the source code:
// tabs - jQuery plugin for accessible, unobtrusive tabs by Klaus Hartl
// http://stilbuero.de/tabs/
// Free beer and free speech. Enjoy!
$.tabs = function(containerId, start) {
var ON_CLASS = 'on';
var id = '#' + containerId;
var i = (typeof start == "number") ? start - 1 : 0;
$(id + '>div:lt(' + i + ')').add(id + '>div:gt(' + i + ')').hide();
$(id + '>ul>li:nth-child(' + i + ')').addClass(ON_CLASS);
$(id + '>ul>li>a').click(function() {
if (!$(this.parentNode).is('.' + ON_CLASS)) {
var re = /([_\\-\\w]+$)/i;
var target = $('#' + re.exec(this.href)[1]);
if (target.size() > 0) {
$(id + '>div:visible').hide();
target.show();
$(id + '>ul>li').removeClass(ON_CLASS);
$(this.parentNode).addClass(ON_CLASS);
} else {
alert('There is no such container.');
}
}
return false;
});
};
Please use the latest version of the plugin: Demo and Download.
You can also download a complete sample, together with some nice CSS, or have a look at this sample live.
Enjoy!
Update: Tabs plugin revised
Update: Bookmarkable Tabs
Update: Tabs Version 2
Update: Support for (unobtrusive) Ajax
Update: Tabs 2.7: Flexibility
274 Comments
Comments feed
Comment by Matthew Delmarter, May 13th, 2006 at 10:24 pm
Author Comment
Comment by Klaus, May 17th, 2006 at 11:44 am
Comment by Cybolic, May 22nd, 2006 at 10:24 pm
Comment by Rob Winters, May 23rd, 2006 at 8:23 am
Author Comment
Comment by Klaus, May 23rd, 2006 at 9:48 am
Comment by Konstantin Dankov, May 26th, 2006 at 12:04 pm
Author Comment
Comment by Klaus, May 26th, 2006 at 2:32 pm
Comment by Konstantin Dankov, May 29th, 2006 at 8:45 am
Comment by Brian Burg, May 31st, 2006 at 2:38 pm
Comment by Matti Putkonen, June 6th, 2006 at 8:18 pm
Comment by TeeCee, June 8th, 2006 at 5:36 pm
Pingback by Labnotes » Blog Archive » links for 2006-06-09, June 10th, 2006 at 12:57 am
Pingback by Kevø X Thomson » Blog Archive » links for 2006-06-12, June 14th, 2006 at 9:54 am
Comment by David Koscinski, June 15th, 2006 at 5:32 pm
Author Comment
Comment by Klaus, June 15th, 2006 at 6:29 pm
Pingback by Klaus Hartl - Stilbüro : Tabs plugin revised, June 18th, 2006 at 6:58 pm
Comment by shinmai, June 27th, 2006 at 1:07 pm
Author Comment
Comment by Klaus, June 27th, 2006 at 1:30 pm
Pingback by 15 Days Of jQuery : jQuery Tabs, June 27th, 2006 at 3:52 pm
Pingback by Klaus Hartl - Stilbüro : Bookmarkable Tabs, July 13th, 2006 at 8:11 pm
Comment by cody lindley, July 27th, 2006 at 5:10 pm
Author Comment
Comment by Klaus, July 28th, 2006 at 8:49 pm
Comment by Harvey Ramer, August 3rd, 2006 at 12:41 am
Comment by Victor Tzar, August 7th, 2006 at 4:53 pm
Author Comment
Comment by Klaus, August 8th, 2006 at 9:50 pm
Comment by rolf, August 23rd, 2006 at 1:56 am
Comment by Andrew, August 24th, 2006 at 2:00 am
Comment by Harpreet, August 29th, 2006 at 9:57 am
Author Comment
Comment by Klaus, August 29th, 2006 at 7:34 pm
Comment by Kit Churchill, September 12th, 2006 at 1:50 am
Comment by Matt, September 25th, 2006 at 10:42 pm
Author Comment
Comment by Klaus, September 26th, 2006 at 8:58 am
Comment by Richard Thomas, September 28th, 2006 at 7:15 pm
Author Comment
Comment by Klaus, September 29th, 2006 at 6:46 am
Comment by Eskil, September 30th, 2006 at 12:40 am
Author Comment
Comment by Klaus, October 1st, 2006 at 6:34 am
Comment by Eskil, October 2nd, 2006 at 2:38 am
Comment by Rolf, October 2nd, 2006 at 6:15 pm
Comment by Sahar, October 7th, 2006 at 12:43 am
Comment by Greg Soulsby, October 12th, 2006 at 9:55 am
Comment by Christian Storgaard, October 25th, 2006 at 1:15 am
Comment by Gunnar, October 26th, 2006 at 2:59 pm
Comment by Gunnar, October 26th, 2006 at 3:47 pm
Author Comment
Comment by Klaus, October 26th, 2006 at 9:09 pm
Comment by Alex, October 30th, 2006 at 5:24 pm
Author Comment
Comment by Klaus, October 30th, 2006 at 10:48 pm
Pingback by links for 2006-10-02 at willkoca, October 31st, 2006 at 11:30 pm
Comment by Anuj Anand, November 1st, 2006 at 6:27 pm
Author Comment
Comment by Klaus, November 1st, 2006 at 10:54 pm
Comment by Roberto, November 2nd, 2006 at 8:07 pm
Author Comment
Comment by Klaus, November 2nd, 2006 at 8:34 pm
Comment by Bantoo05, November 2nd, 2006 at 10:48 pm
Pingback by Klaus Hartl - Stilbüro : Tabs Version 2, November 6th, 2006 at 11:19 am
Comment by Roberto, November 7th, 2006 at 7:26 pm
Comment by Knalpiep, November 16th, 2006 at 5:37 pm
Author Comment
Comment by Klaus, November 16th, 2006 at 6:31 pm
Comment by Knalpiep, November 17th, 2006 at 8:55 am
Author Comment
Comment by Klaus, November 17th, 2006 at 9:17 am
Comment by Nerique, November 17th, 2006 at 12:10 pm
Comment by Knalpiep, November 17th, 2006 at 12:13 pm
Author Comment
Comment by Klaus, November 17th, 2006 at 12:46 pm
Comment by Dave, November 28th, 2006 at 3:55 pm
Comment by Knalpiep, November 28th, 2006 at 8:15 pm
Author Comment
Comment by Klaus, November 28th, 2006 at 10:41 pm
Author Comment
Comment by Klaus, November 30th, 2006 at 11:41 am
Comment by Dave, November 30th, 2006 at 4:08 pm
Author Comment
Comment by Klaus, November 30th, 2006 at 4:45 pm
Comment by Dave, November 30th, 2006 at 5:04 pm
Author Comment
Comment by Klaus, November 30th, 2006 at 5:16 pm
Comment by Jared, December 7th, 2006 at 1:17 am
Author Comment
Comment by Klaus, December 7th, 2006 at 1:49 pm
Comment by Jared, December 11th, 2006 at 1:28 am
Author Comment
Comment by Klaus, December 11th, 2006 at 11:13 am
Comment by Jared, December 12th, 2006 at 3:00 am
Comment by Josh Giese, December 13th, 2006 at 6:47 pm
Pingback by Web Expose » JQuery Javascript library, December 18th, 2006 at 3:16 pm
Comment by Stephen Jensen, January 10th, 2007 at 10:42 pm
Comment by James Norton, February 1st, 2007 at 12:24 am
Author Comment
Comment by Klaus, February 1st, 2007 at 12:00 pm
Comment by Mikael, February 6th, 2007 at 7:33 pm
Author Comment
Comment by Klaus, February 6th, 2007 at 7:55 pm
Comment by Jason, February 7th, 2007 at 6:20 am
Author Comment
Comment by Klaus, February 7th, 2007 at 8:42 am
Comment by Mikael, February 8th, 2007 at 10:58 pm
Author Comment
Comment by Klaus, February 9th, 2007 at 1:50 am
Comment by Stephen Jensen, February 12th, 2007 at 4:54 pm
Author Comment
Comment by Klaus, February 12th, 2007 at 6:48 pm
Comment by Mikael, February 15th, 2007 at 11:56 am
Comment by Mikael, February 16th, 2007 at 1:17 am
Author Comment
Comment by Klaus, February 16th, 2007 at 6:18 am
Comment by Greg, February 20th, 2007 at 8:37 pm
Author Comment
Comment by Klaus, February 20th, 2007 at 9:43 pm
Comment by Tambietcuocdoi, February 21st, 2007 at 10:48 pm
Author Comment
Comment by Klaus, February 21st, 2007 at 11:30 pm
Comment by Tambietcuocdoi, February 24th, 2007 at 8:33 am
Comment by sebastian, February 28th, 2007 at 11:05 pm
Comment by leen, March 2nd, 2007 at 2:35 pm
Comment by Rack Lin, March 8th, 2007 at 3:11 am
Pingback by Tab Interface Scripts | Dr. Web Weblog, March 18th, 2007 at 6:03 pm
Comment by Matteo, March 19th, 2007 at 12:24 pm
Author Comment
Comment by Klaus, March 19th, 2007 at 2:32 pm
Comment by Matteo, March 19th, 2007 at 2:58 pm
Comment by kevin, March 19th, 2007 at 5:50 pm
Comment by Tom Webdesign Traunstein, March 21st, 2007 at 11:16 pm
Comment by Alex, March 22nd, 2007 at 12:09 pm
Author Comment
Comment by Klaus, March 22nd, 2007 at 5:01 pm
Comment by Matteo, March 23rd, 2007 at 4:14 pm
Comment by Christian, March 27th, 2007 at 5:24 pm
Comment by Matthew, March 27th, 2007 at 7:11 pm
Author Comment
Comment by Klaus, March 27th, 2007 at 9:17 pm
Author Comment
Comment by Klaus, March 27th, 2007 at 9:24 pm
Comment by Christian, March 28th, 2007 at 12:15 am
Comment by kevin, March 30th, 2007 at 1:47 am
Comment by Peter, March 30th, 2007 at 10:09 am
Comment by Svilen Sabev, March 30th, 2007 at 8:30 pm
Author Comment
Comment by Klaus, March 31st, 2007 at 12:56 am
Comment by Peter, April 1st, 2007 at 1:58 pm
Author Comment
Comment by Klaus, April 1st, 2007 at 9:45 pm
Comment by Ivanko, April 2nd, 2007 at 11:20 am
Author Comment
Comment by Klaus, April 2nd, 2007 at 12:37 pm
Comment by Ivanko, April 2nd, 2007 at 4:26 pm
Author Comment
Comment by Klaus, April 2nd, 2007 at 4:38 pm
Comment by Ajax4Me, April 4th, 2007 at 4:07 am
Comment by Beurt, April 10th, 2007 at 6:17 pm
Author Comment
Comment by Klaus, April 10th, 2007 at 6:53 pm
Comment by Beurt, April 10th, 2007 at 8:25 pm
Comment by Paolo, April 12th, 2007 at 2:43 pm
Pingback by 14 Tab-Based Inferface Techniques | Smashing Magazine, April 17th, 2007 at 5:17 pm
Pingback by Webdesign (css, grafica e altro) » Blog Archive » 14 Tab-Based Interface Techniques, April 17th, 2007 at 8:26 pm
Comment by Matt Richards, April 18th, 2007 at 9:51 am
Author Comment
Comment by Klaus, April 18th, 2007 at 11:15 am
Author Comment
Comment by Klaus, April 18th, 2007 at 11:19 am
Pingback by Menu de abas com jQuery, April 18th, 2007 at 5:36 pm
Pingback by sekme bazli arayuz teknikleri : İnternet ve Yaşam, April 19th, 2007 at 3:20 pm
Comment by Castellan, April 19th, 2007 at 6:06 pm
Comment by Plant for the Planet, April 19th, 2007 at 8:26 pm
Pingback by CMP2.NET | Design,Multimedia,Internet,Security | » Blog Archive » Sekme bazlı arayüz teknikleri, April 20th, 2007 at 3:04 pm
Comment by Gonzalo, April 21st, 2007 at 8:46 pm
Pingback by Menu com onmouseover, April 25th, 2007 at 8:29 pm
Comment by Ross, April 26th, 2007 at 10:33 am
Pingback by by Webciyiz Webmaster Kaynakları, April 26th, 2007 at 11:55 am
Pingback by Tab Çeşitleri - Tab Arayüz by Webciyiz Webmaster Kaynakları, April 26th, 2007 at 11:56 am
Comment by Patrick, May 5th, 2007 at 4:13 pm
Comment by tom, May 8th, 2007 at 4:36 pm
Comment by tg, May 8th, 2007 at 10:54 pm
Comment by Craig, May 11th, 2007 at 3:23 pm
Comment by Basic IFrame Support added, May 14th, 2007 at 10:35 pm
Comment by Basic IFrame Support added, May 14th, 2007 at 10:36 pm
Author Comment
Comment by Klaus, May 15th, 2007 at 9:14 am
Comment by suresh, May 18th, 2007 at 8:35 am
Comment by Gary F, May 20th, 2007 at 9:16 pm
Comment by Mike, May 22nd, 2007 at 12:41 pm
Author Comment
Comment by Klaus, May 22nd, 2007 at 1:41 pm
Author Comment
Comment by Klaus, May 22nd, 2007 at 1:42 pm
Comment by Gary F, May 23rd, 2007 at 2:15 pm
Author Comment
Comment by Klaus, May 23rd, 2007 at 2:44 pm
Comment by Sean, May 23rd, 2007 at 3:17 pm
Author Comment
Comment by Klaus, May 24th, 2007 at 10:37 am
Comment by Gary F, May 24th, 2007 at 2:48 pm
Comment by Sandro, May 25th, 2007 at 10:10 am
Author Comment
Comment by Klaus, May 25th, 2007 at 10:44 am
Comment by Sandro, May 25th, 2007 at 10:53 am
Author Comment
Comment by Klaus, May 25th, 2007 at 1:44 pm
Comment by Sandro, May 25th, 2007 at 2:18 pm
Comment by Gary F, May 26th, 2007 at 7:00 pm
Comment by Ritesh Agrawal, May 29th, 2007 at 7:29 am
Comment by David, June 1st, 2007 at 10:57 am
Comment by pratheesh kumar, June 2nd, 2007 at 10:41 pm
Comment by Cynthia, June 3rd, 2007 at 12:37 am
Comment by Cynthia, June 3rd, 2007 at 1:04 am
Comment by Cynthia, June 3rd, 2007 at 8:08 pm
Pingback by 26 Best ways to implement AJAX, CSS and Javascript based Tabs at Witty Sparks, June 9th, 2007 at 8:09 pm
Pingback by Bookmarks » Blog Archive » 26 Best ways to implement AJAX, CSS and Javascript based Tabs, June 9th, 2007 at 9:17 pm
Comment by MG, June 11th, 2007 at 4:34 pm
Author Comment
Comment by Klaus, June 11th, 2007 at 8:45 pm
Pingback by links for 2007-06-12 « /tmp, June 12th, 2007 at 5:18 pm
Pingback by jQuery plugin | codelinkers, June 12th, 2007 at 8:26 pm
Pingback by WebAdictos - Una dosis diaria de web@ » Archivo del Blog » 26 Maneras de implementar tabs (pestañas) con AJAX, CSS y Javascript, June 19th, 2007 at 1:55 am
Comment by Hendra-1, June 19th, 2007 at 8:50 am
Comment by Per, June 19th, 2007 at 1:52 pm
Comment by Chacon, June 21st, 2007 at 9:30 pm
Author Comment
Comment by Klaus, June 22nd, 2007 at 10:31 am
Comment by Andre, June 25th, 2007 at 3:12 pm
Comment by Andre, June 25th, 2007 at 3:17 pm
Comment by Julien, June 27th, 2007 at 4:48 pm
Author Comment
Comment by Klaus, June 28th, 2007 at 6:51 am
Pingback by Codigo Geek » Blog Archive » 24 alternativas para Tab o Pestañas, July 1st, 2007 at 9:36 pm
Comment by Brian, July 3rd, 2007 at 2:18 pm
Comment by Mathijs Gaalman, July 3rd, 2007 at 9:54 pm
Comment by Aldo, July 4th, 2007 at 1:55 pm
Comment by DavidD00, July 4th, 2007 at 6:14 pm
Comment by djot, July 5th, 2007 at 7:18 am
Comment by djot, July 5th, 2007 at 7:25 am
Comment by djot, July 5th, 2007 at 7:54 am
Author Comment
Comment by Klaus, July 5th, 2007 at 10:01 am
Comment by Liverpool, July 6th, 2007 at 8:46 am
Comment by dizor, July 12th, 2007 at 8:53 pm
Comment by dizor, July 12th, 2007 at 9:19 pm
Comment by João Araújo, July 13th, 2007 at 5:46 pm
Comment by João Araújo, July 13th, 2007 at 9:50 pm
Pingback by CaudalWeb Blog » Blog Archive » Interfaces basadas en Tabs, July 14th, 2007 at 7:59 am
Comment by Saeed, July 14th, 2007 at 2:08 pm
Comment by Karthik, July 18th, 2007 at 4:55 pm
Author Comment
Comment by Klaus, July 18th, 2007 at 9:34 pm
Comment by Chris, July 25th, 2007 at 10:54 pm
Author Comment
Comment by Klaus, July 26th, 2007 at 9:18 am
Comment by andy, July 26th, 2007 at 8:11 pm
Comment by Matthew, July 27th, 2007 at 4:02 pm
Comment by d00bie, July 28th, 2007 at 4:21 pm
Comment by camilord, July 28th, 2007 at 5:30 pm
Author Comment
Comment by Klaus, July 28th, 2007 at 6:37 pm
Comment by Andy, July 29th, 2007 at 7:08 pm
Author Comment
Comment by Klaus, July 30th, 2007 at 5:25 pm
Comment by Martin, July 31st, 2007 at 8:26 pm
Comment by Martin, July 31st, 2007 at 9:42 pm
Comment by Marjan, August 1st, 2007 at 2:42 pm
Comment by massa, August 1st, 2007 at 5:30 pm
Author Comment
Comment by Klaus, August 1st, 2007 at 6:25 pm
Comment by Marjan, August 2nd, 2007 at 9:38 am
Comment by Neil, August 7th, 2007 at 9:52 pm
Comment by KAB, August 8th, 2007 at 3:48 pm
Comment by KAB, August 8th, 2007 at 3:52 pm
Pingback by David’s Thoughts » Blog Archive » links for 2007-08-09, August 9th, 2007 at 5:27 am
Comment by samarth, August 9th, 2007 at 6:22 pm
Comment by dom, August 10th, 2007 at 6:02 pm
Author Comment
Comment by Klaus, August 11th, 2007 at 9:38 am
Comment by Marco, August 14th, 2007 at 9:44 am
Comment by Marco, August 14th, 2007 at 9:46 am
Comment by Christian Zange, August 16th, 2007 at 1:15 pm
Comment by Swieczkos, August 23rd, 2007 at 9:25 am
Comment by Marc-Aurèle Brothier, August 23rd, 2007 at 10:56 am
Comment by Herman Tolentino, August 25th, 2007 at 11:11 pm
Comment by Alex, September 2nd, 2007 at 7:37 am
Author Comment
Comment by Klaus, September 2nd, 2007 at 12:42 pm
Comment by Gary, September 2nd, 2007 at 8:30 pm
Comment by Alex, September 3rd, 2007 at 2:34 pm
Author Comment
Comment by Klaus, September 3rd, 2007 at 2:52 pm
Comment by Alex, September 5th, 2007 at 3:37 pm
Author Comment
Comment by Klaus, September 6th, 2007 at 10:02 am
Comment by Michael, September 8th, 2007 at 7:51 pm
Comment by Rogers, September 10th, 2007 at 9:28 pm
Author Comment
Comment by Klaus, September 11th, 2007 at 10:04 am
Comment by rogers, September 11th, 2007 at 7:13 pm
Comment by kali das sharma, September 11th, 2007 at 7:51 pm
Comment by kali das sharma, September 11th, 2007 at 7:56 pm
Comment by Christopher Vrooman, September 11th, 2007 at 11:33 pm
Author Comment
Comment by Klaus, September 11th, 2007 at 11:47 pm
Comment by Ryno, September 12th, 2007 at 5:12 am
Comment by AuSer, September 12th, 2007 at 7:40 am
Author Comment
Comment by Klaus, September 12th, 2007 at 10:29 am
Comment by Petros, September 12th, 2007 at 2:32 pm
Author Comment
Comment by Klaus, September 12th, 2007 at 2:47 pm
Comment by Ryno, September 13th, 2007 at 2:17 am
Comment by Petros, September 13th, 2007 at 7:42 am
Author Comment
Comment by Klaus, September 13th, 2007 at 11:42 am
Comment by Petros, September 13th, 2007 at 2:49 pm
Author Comment
Comment by Klaus, September 13th, 2007 at 7:48 pm
Comment by Daniel, September 14th, 2007 at 10:34 am
Comment by Anand, September 16th, 2007 at 1:04 am
Comment by Aaron, September 17th, 2007 at 6:50 am
Comment by Petros, September 17th, 2007 at 8:40 am
Comment by Randy, September 17th, 2007 at 3:57 pm
Comment by Christian Ready, September 19th, 2007 at 7:18 pm
Comment by Justin, September 20th, 2007 at 10:42 pm
Comment by Luki, September 22nd, 2007 at 12:46 am
Comment by chenshian, September 26th, 2007 at 9:30 am
Comment by Arnold, September 27th, 2007 at 2:42 pm
Comment by jurciks, September 27th, 2007 at 3:55 pm
Comment by Shiloh Madsen, September 28th, 2007 at 7:51 pm
Comment by Max, October 2nd, 2007 at 9:28 am
Comment by john, October 2nd, 2007 at 4:06 pm
Comment by Andy, October 2nd, 2007 at 7:10 pm
Comment by Sulman Bhatti, October 11th, 2007 at 4:54 pm
Pingback by Useful Web 2.0 Designs and Techniques, October 21st, 2007 at 8:48 pm