93 responses to “How To Create Tabs Using jQuery”

  1. Karl

    Hi Justin,

    Nice tutorial! One way you could reduce the code a bit is to show the correct div based on the class of the clicked tab. That way you wouldn’t have to repeat the same basic thing four times. Here is what it would look like:

    $(document).ready(function() {
    // setting the tabs in the sidebar hide and show, setting the current tab
    $('div.tabbed div').hide();
    $('div.t1').show();
    $('div.tabbed ul.tabs li.t1 a').addClass('tab-current');

    // SIDEBAR TABS

    $('div.tabbed ul li a').click(function(){
    var thisClass = this.className.slice(0,2);
    $('div.tabbed div').hide();
    $('div.' + thisClass).show();
    $('div.tabbed ul.tabs li a').removeClass('tab-current');
    $(this).addClass('tab-current');
    });
    });

  2. Kristian

    Hi justin.
    Again, this is great. I have downloaded the updated visionary theme, and thanks to this tutorial i have actually managed to get TAGS displayed where the archives, popular post and so on are.

    Feeling like a champ here after reading the tutorial.

  3. Justin

    Karl, thank you. I knew there was an easier way because I was wondering what I’d do if I ever had a script where I had 10 tabs or so. That would get a bit out of hand. I’m about to update the code.

    Kristian, I’m glad I could help and that you’ve got your tabs working like you want them.

  4. Ozh

    Nice explanations & tutorial. One thing I miss though with this is more correctly structured markup, that makes sense when JS is off on when printed, for instance.

    At the moment you have:
    <ul>
    <li>tab1</li>
    <li>tab2</li>
    <li>tab3</li>
    </ul>
    <div id="div1">stuff</div>
    <div id="div2">stuff</div>
    <div id="div3">stuff</div>

    Something that would make more sense would be:
    <ul>
    <li>tab1
    <div id="div1">stuff</div>
    </li>


    <li>tab2
    <div id="div2">stuff</div>
    </li>


    <li>tab3
    <div id="div3">stuff</div>
    </li>
    </ul>

  5. od3n

    i use it also in my blog. try take a look. i did change the css also. but i’ve problems with css. it will float on left if there’s no content on left. help me please. dont understand the id and class u use in the page.

  6. Mani Karthik

    Good work Justin. One question – would the links be spiderable?

  7. Justin

    Od3n, I’m not sure exactly what you’re talking about. Everything looks pretty good to me.

    Mani, thanks. I believe the links would be. That’s not my area of expertise. The links are just simple HTML links. So, if those are spiderable, then I suppose these are.

  8. od3n

    @Justin try use search in my blog to find something not there for example, type xx. u\’ll see some broken style. where to modify it? sorry but i\’m just noob. and how to change the css style of the tabbed content? wanna it look like my side bar style with cube numbering style. erm. can give me ur email?

  9. Justin

    Od3n, this float issue has nothing to do with this script. This is a CSS mistake that you’ve made somehow. I would suggest structuring it or changing your CSS to fit with your theme. The CSS is left open so that you can modify it to fit with your site. This is a tutorial for people with a more advanced knowledge of CSS and XTHML.

    I know English may not be your first language, but for the sake of my sanity, please capitalize your sentences and try to use some form of proper grammar. :)

  10. od3n

    @Justin Thanks. I’ll try to improve my english.

  11. Bob Dunne

    Hi Justin,
    I have no design experience and am wondering how do i put the php files sidebar, style and header into a html file to test out and see it working.
    Thanks
    Bob

  12. Justin

    Bob, I’m not exactly sure what you’re asking because you don’t put these into an HTML file. You open the template files from your theme that I mentioned in the tutorial.

    If CSS and XHTML are totally unfamiliar territory for you, then this really isn’t a tutorial you should be following. This isn’t exactly an advanced tutorial, but it’s definitely not for beginners. I can point you toward some good starting places though.

    Try to leave some more specific details about what your problem is, and I’ll try my best to help out.

  13. IngaOz

    I was looking for tutorial like this for week now, glad that I found it finally! It looks a bit complicated though… However, I’ll try to understand… :) Thanks again!

  14. IngaOz

    Oh well, something is not working for me – it shows 1st table and don’t let me click on tab2 or tab3 etc… I give up for tonight it’s already 5AM here, will try sort it out Tomorrow probably.

  15. Justin

    IngaOz, I hope you get it working.

    One minor note: please don’t leave a link to your feed in the website field when commenting. This unintentionally gets users to subscribe to your feed. It’s something I won’t allow on JustinTadlock.com.

  16. Rene

    Thanks a ton for this tutorial! It is exactly what I was looking for. I was able to use it on a static page very easily. GREAT JOB! :)

  17. Jonah Dempcy

    My colleague Alex recently posted his take on creating tabs with jQuery. You can compare the implementation here:

    http://www.thetruetribe.com/2008/04/how-do-i-make-tabs-in-jquery-without-jq.html

  18. remeinic

    thanks for the tutorial.. this help me a lot.

  19. Hungzai

    Great tutorial! But i am facing a problem. Problem is it isn’t producing tabs for me, instead, it is producing a

    Tab 1
    Tab 2
    Tab 3
    Tab 4

    with results below all that.

    What did i do wrong my dear?

  20. Hungzai

    Ok, got it. It ain’t working in OPERA.

  21. Hungzai

    I’m done. Took me a lot of time to get it working. And still not perfect, a little cranky. Anyway it’s a good start. Thanks for your article.

  22. Hungzai

    If you look at my site in firefox, there appears a dot beside my tabs, it doesn’t in IE or OPERA. Is that a bug ?

  23. Hungzai

    One more question, how to i center the list of tabs? I have tried everything and it doesn’t. It will float to the left. The only way to do this is to place them inside tables but even then, they don’t center inside cells, you have to adjust the cells according to the length of the tabs, but problem with that is every browser are displaying different lengths and as such it doesn’t work in OPera.

    I wonder how people manage to center those tabs? Please share your valuable techniques! Thanks in advance!

  24. Hungzai

    Another problem is that cannot operate 2 tabs in the same template. When one is working, it would disable the other. Or did i overlook something?

  25. Hungzai

    Ok, it’s all up and working perfectly now. For those who needed an example here’s mine http://www.hungzai.com/red-house/

    There are a few issues i encountered in drilling it to perfection.

    1) There is a bug in firefox where if you have a list items in one of your tab’s content, the first item on the list would jump out of the content box(container). The solution is to place a float: left in .tabbed ul.tabs

    2) There is no way you can center the row of tabs. It just doesn’t work, whether you wanna float it or align it or even if you’re placing them inside a cell in a table, they would be aligned left. I’m not sure why but the solution for me is first, put everything in a cell and adjust the cell to the right length(that is before the last tab returns the next row). Bear in mind, at this point there would still be a little leftover spaces on the right, most likely to be something betweeen 5px to 10px. Next what i do is to place a left border in .tabbed ul.tabs to counter the right spaces. If you want it to look more perfect, you could do the top and bottom too.

    Lastly if you decide to use them in a table, you CANNOT place the codes in different cells, they just don’t work and cut off from each other. You have to place them in one single cell. It’s for alignment purposes only anyway if you decide to use them on a page, for sidebars i guess you can do without one since it would be auto-adjusted. Good luck!

  26. Hungzai

    Oops sorry! I didn’t know you have a forum for it, i apologise for flooding your comments area. Feel free to delete my comments here!

  27. Reyner

    nice tutorial. I’m learning how to do a box with some links using tabs for different categories with jquery.. I google you! so i use some part of your tutorial and some part of the jquery documentation to have a better idea for the jquery tabs

    thanks again!

  28. Help Developer - CSS, Dreamweaver, Gimp, HTML, JavaScript, PHP, VB.NET, Visual Studio, Wordpress Tutorials » Blog Archive » Create a tabbed AJAX area in Wordpress

    [...] This effect is achieved with a mix of Javascript and CSS and in this tutorial I will show you how to get it set up on your site, I will be using three tabs like on Help Developer, these will be Recent Posts, Categories and Archives. This tutorial is an extension of Justin Tadlock’s post here. [...]

  29. Wordpress Island - Wordpress Themes, Plugins, News, Forums, Articles » Blog Archive » TUTORIAL: Create a tabbed AJAX area in Wordpress

    [...] This effect is achieved with a mix of Javascript and CSS and in this tutorial I will show you how to get it set up on your site, I will be using three tabs like on Help Developer, these will be Recent Posts, Categories and Archives. This tutorial is an extension of Justin Tadlock’s post here. [...]

  30. Asuka

    Does it still work with wordpress 2.6? I get the tabs and css ok. Everything looks nice nut nothing happends when I click a tab?

  31. Create a tabbed AJAX area in Wordpress - Blue Box Sols

    [...] This effect is achieved with a mix of Javascript and CSS and in this tutorial I will show you how to get it set up on your site, I will be using three tabs like on Help Developer, these will be Recent Posts, Categories and Archives. This tutorial is an extension of Justin Tadlock’s post here. [...]

  32. Danh ba web 2.0

    Thanks tutorial
    I like it very much !

  33. J Mehmett

    Hi Justin,

    I was testing the above tutorial to implement it with a WordPress theme, I have seen a misspelling in the CSS part, which may lead to some confusion. Here is the code I’m up to:

    .tabbed ul.tabs li {
    list-style-typ: none;
    float: left;
    margin: 0;
    padding: 0;
    }

    Note: list-style-typ:, where it was supposed to be list-style-type: . An “e” is missing.

    Good tutorial, though. Thanks for sharing.

  34. mwpwalshe

    If I use a Hello inside the tab it does not display any content in latest firefox? How can I use within the tab div??Thanks
    mwpwalshe

  35. mwpwalshe

    whoops looks like Im missing “”div”" tabs 4m above I will try again!
    If I use a div Hello /div inside the tab – It does not display any content in the latest fiorefox??? How can I use the div /div tag within the tab div?

  36. Divya

    Aye Justin,

    Thanks a lot, step by step explanation really helped me,
    I’m a PHP developer and I’m very new to Design stuff.
    This was quite helpful.

  37. The Power of WordPress and jQuery: 30+ Useful Plugins & Tutorials

    [...] 19. How To Create WordPress Tabs Using jQuery [...]

  38. Wordpress Blog Services - The Power of WordPress and jQuery: 25+ Useful Plugins & Tutorials

    [...] 19. How To Create WordPress Tabs Using jQuery [...]

  39. indojepang

    in tab 3 example you put a div in it.. but it won’t show up.. cos it hidden?
    how to show this div?

  40. The Power of WordPress and jQuery: 25+ Useful Plugins & Tutorials | @article @lbum

    [...] 19. How To Create WordPress Tabs Using jQuery [...]

  41. Internet Blog Addict » 9 awesome Jquery plugins

    [...] 5. Tabbed side bar [...]

  42. Free WordPress and jQuery: 25+ Useful Plugins & Tutorials | guidesigner.com

    [...] 19. How To Create WordPress Tabs Using jQuery [...]

  43. Dan

    Hi Justin, this is a great post. I got it to work immediately but I’ve come across a problem. I have set the tabs to load conditionally, checking to see if a certain custom field exists or not, and it works great if the first tab is supposed to load along with others. But when it happens that the first tab is not needed and other tabs are, it would show the tabs well, yet fail to load automatically unless I click on them.

    I see that the tabs.js code tells the first tab to stay open, but do you think there’s other ways to let other tabs load in case the first one is not needed? For example, on my site, I am trying to put up multiple versions of videos.

    Thanks

  44. Ville Walveranta

    This was very useful! Thanks!

    I implemented tabs based on this example after having first tried jQuery UI Tabs which didn’t allow separating the tabs container from the corresponding content divs. Using this example as a starting point it was a piece of cake! :-)

  45. Yanyan

    dude, your my savior… this tutorial really helped me with my project… BWAHAHA thanks man!!!

  46. Femin

    Nice Tutorial

  47. Josh Lewis

    Well I used your tutorial and loved it and it worked great, but, when I you rollover the tabs the cursor thinks it is text but as we know its really a tab thats clickable. I didn’t read all these comments in case someone else already posted a solution but I used this:

    .tabs a:hover, .anchors a:focus, .anchors a:active {
    cursor: pointer;
    }

    And it worked great.

  48. Free WordPress and jQuery: 25+ Useful Plugins & Tutorials | designersmantra.com

    [...] 19. How To Create WordPress Tabs Using jQuery [...]

  49. The Power of WordPress and jQuery: 25+ Useful Plugins & Tutorials | forcto.com

    [...] 19. How To Create WordPress Tabs Using jQuery [...]

  50. Web Design Glasgow

    Hi Justin,
    I had having to design anything that scrolls, but alas sometimes this is not possible. So thanks for the heads up on tabbing. I am sure that visitors would much prefer this.

    thanks again
    George

  51. Shooting in Cornwall

    This is fantastic, i’ve been working on a way to make our new website more web 2.O and adding tabs for the blog sliding to different categories will be very nice. Great work. Dave

  52. Doug Hogan » daily life

    [...] How To Create Tabs Using jQuery — 1:32pm via [...]

  53. ExpertSoul

    Hi Justin,

    Great article! I started working with tabs recently and having issue with events and disabled tabs.

    At page load I am attaching click event to the tabs and disabling few of the tabs. Even if tabs are disabled, click is still firing. Is there a way to find out if clicked tab is disabled so that I can avoid doing regular operations.

    Thanks.

  54. raja

    i have this kind of error during runtime what can i do
    please help me
    Fatal error: Call to undefined function: _e() in C:\Program Files\xampp\htdocs\tab\sidebar.php

  55. bubba

    @raja

    you probably don’t have wordpress installed. Just replace with the plain text “Tab 1″. Do it for all of them.

    @justin thanks for the write up. Totally answered a question for me. One note: Basing this on the tab’s href would allow it to work as a ‘jump list’ when JS is off. As it is, this will just leave a dead list of non-links on the page when JS is unavailable. Tab 1 and then content.

  56. bubba

    wow. comment code removal fail. sorry, dunno how to get it in.

  57. Sumit

    hi there

    is it possible to use more of these tabs on one single site without copy & paste the code over and over again and include the same .js with a different name?

    Like
    ———-
    Tab1 Tab2 Tab3
    TABcontent1
    ———

    Tab3 Tab4 Tab5
    Tabcontent2
    ———

    and so on

    possible?

  58. jQuery Wordpress Plugins Strikes Again | WPThemesPlugin.com

    [...] How To Create Tabs Using jQuery Tutorial. Tabbed navigation is a bonus as it gives quick and easy access to various parts of your site. This tutorial will enable you to build and design your own tabs, and make you the envy of all your blogging friends. [...]

  59. Disha Hosting » jQuery Wordpress Plugins Strikes Again

    [...] How To Create Tabs Using jQuery Tutorial. Tabbed navigation is a bonus as it gives quick and easy access to various parts of your site. This tutorial will enable you to build and design your own tabs, and make you the envy of all your blogging friends. [...]

  60. 30 Tutorials Combining Both Wordpress and jQuery : Speckyboy Design Magazine

    [...] How To Create Tabs Using jQuery [...]

  61. Lista de Tutoriais combinando JQuery e Wordpress: turbine seu blog | Blog Tecnologia e Educação

    [...] Este tutorial ensina a criar abas na barra lateral do blog. [...]

  62. 30 Tutorials Combining Both Wordpress and jQuery | huibit05.com

    [...] How To Create Tabs Using jQuery [...]

  63. paul whitehead

    this was awsome, but when i turned off the JS for accessability testing, it hide the content, which isnt so good.

    It was easy to fix though, by adding the display none class when the js is executed (the last line was added )

    $(document).ready(function() {
    // setting the tabs in the sidebar hide and show, setting the current tab
    $(‘div.tabbed div’).hide();
    $(‘div.t1′).show();
    $(‘div.tabbed ul.tabs li.t1 a’).addClass(‘tab-current’);
    $(‘.tabbed div.t2, .tabbed div.t3, .tabbed div.t4′).addClass(‘tabbed_hidden’); //here.
    //this line means that the tabs are hidden by js not css on load so when js is disabled the divs are shown.

    but then the class must be removed when the user clicks the ul li a link…. like so…

    // SIDEBAR TABS
    $(‘div.tabbed ul li a’).click(function(){
    var thisClass = this.className.slice(0,2);
    $(‘div.tabbed div’).hide();
    $(‘div.’ + thisClass).show();
    $(‘div.tabbed ul.tabs li a’).removeClass(‘tab-current’);
    $(this).addClass(‘tab-current’);
    $(this).removeClass(‘tabbed_hidden’); //here
    //this just removes the class we added before
    });
    });

    and the css

    .tabbed_hidden
    {
    display:none;
    }

    i hope this helps anyone who wants to make their websites accessable.

  64. sandu

    I really liked your tutorial ! I consider it the most practical example i found. I use it everytime i need it ! Thanks for sharing your knowledge! :)

  65. 30 Tutorials Combining Both Wordpress and jQuery | .::tek3D Weblog::.

    [...] How To Create Tabs Using jQuery [...]

  66. Customizing WordPress With JQuery | WordPress eCommerce

    [...] View Tutorial [...]

  67. vicky

    hi ,
    i m new one. so can u tell me how can i implement this code in my page.

  68. Hara Kim

    Thanks man this worked perfectly on my site after I cleared my cache lol..

  69. Philippine Outsourcing

    ey, thanks for this tutorial … been searching for the right one

  70. 30 Tutorials Combining Both jQuery and Wordpress

    [...] How To Create Tabs Using jQuery [...]

  71. 30 Tutorials for Using JQuery in Wordpress | oOrch Blog

    [...] How To Create Tabs Using jQuery [...]

  72. 40+ Quite Useful Wordpress Plugins using jQuery | tripwire magazine

    [...] How To Create WordPress Tabs Using jQuery [...]

  73. Mohamed Amine

    Hello, i have enjoyed this tutorial. Much appreciated.

  74. Adam Bourg » 9 awesome Jquery plugins

    [...] 5. Tabbed side bar [...]

  75. How To Create Tabs Using jQuery for Wordpress | bijusubhash.com

    [...] Justin Tadlock comes with a tutorial about How to Create Tabs Using JQuery for Wordpress. This tutorial is primarily based off WordPress, but can easily be ported anywhere else. You can create a tabs section in the wordpress sidebar. It’s a popular and useful feature in Wordpress themes. [...]

  76. chris

    thanks..it’s good tutorial..i have tried it and it works heheh ..

  77. Nicolas

    Old now, but good tutorial :)
    To everybody who is strugging to make it working with the latest version of wordpress, you just have to replace the “$” sign in the tabs.js to “jQuery” as Wordpress built-in jQuery doesn’t recognize the dolalr sign, but the jQuery name.

    :)

    I think you should mention it in your blog post, as it will save time to people trying to make it working with the latest wordpress isntallations.

    Cheers,
    Nicolas.

  78. Elmer

    yeah, thanks Nicolas you save my day!!

  79. How To Create Tabs Using jQuery « Ajax Freak

    [...] View Tutorial » This tutorial is primarily for WordPress, but can easily be ported anywhere else. You will be creating a tabs section within the sidebar, a popular and useful feature in Wordpress themes. VN:F [1.7.7_1013]please wait…Rating: 0.0/10 (0 votes cast)VN:F [1.7.7_1013]Rating: 0 (from 0 votes)Share it Tags: Jquery, tabs, wordpress This entry was posted on December 13, 2009 at 3:14 am, and is filed under Jquery, wordpress. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. [...]

  80. Paul @ HeadwayTips

    @nicholas – you’re right – this took me a while to figure out. To explain it a little more – wordpress now loads jQuery in no-conflict mode – which means you can’t use the $ shortcut.

    But if you use jQuery(document).ready(function($) then you can leave the $ within the function..

    http://docs.jquery.com/Using_jQuery_with_Other_Libraries

    cheers, pc

  81. SHASHANK

    hey m unable to find tab.js file could you help me out plz
    i searched for it,
    it might seem funny to you guys,but i am a newbie here
    anyine to help me out ???

  82. The Power of WordPresjQuerys and : 25+ Useful Plugins & Tutorials | Umraz Ahmed | The Official site

    [...] 19. How To Create WordPress Tabs Using jQuery [...]

  83. scandell

    Many here are right. Currently you are unable to declare any DIV classes under each tab. They simply do not show up.

  84. 30 Tutorials Combining Both Wordpress and jQuery | Coyot [at] CanalCoffee.com : Plugins

    [...] How To Create Tabs Using jQuery [...]

  85. jay

    i m looking for jquery tabs and will try ur code
    thanks

  86. 12 Amazing jQuery Tabbed Interface Plugins and Tutorials « Defonic International Solutions

    [...] 5. How To Create Tabs Using jQuery [...]

  87. Sayontan

    Justin,
    Thanks for this tutorial. I took the concept explained in here a step further and combined it with a few more JQuery functions. The result was a natively tabbed sidebar. So whatever widgets you add to your sidebar get displayed as tabs.

    It was an interesting problem to think about, because the WP sidebar parameters $before_widget, $after_widget, $before_title and $after_title don’t lend themselves naturally to the markup you proposed above. You can see the writeup on my approach if you please.

    Cheers,
    Sayontan.

  88. Mark

    Great tutorial, really simple to use.

    Have adapted the code a small bit in order to create a tabbed area on a page which people are able to switch between.

    However i would like to know if it is possible using this tutorial to link directly to a specific tab.

  89. 31 Great WordPress jQuery Resources | The Designer's Pod

    [...] WordPress Tabs using jQuery [...]

  90. How To Create WordPress Tabs Using jQuery | jQuery Wisdom

    [...] This tutorial is primarily based off WordPress, but can easily be ported anywhere else. Web Site Demo Share and [...]

Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.