Subscribe to our RSS

How to Create Custom AJAX Tabs using jQuery

Tags: , , , ,

How to Create Custom AJAX Tabs using jQuery

Today’s morning started a little hot here at ajaxBlender. We had some free time before starting the production meeting, and Mike (our lead developer guy) and Justin (newbie who joined us in November) started arguing about their respective abilities of creating cross-browser markup code (HTML, CSS, JS) by hand without mistakes, but also doing it really fast.The exchange went a little like this:

J: I am sure, there is no way you can create scripts from scratch fast. First thing is you integrate the 3rd party stuff. This is easy but when you have to create your custom code, it will require time for debugging, planning the structure, etc – even for small scripts.

M: Ha, do you want to try me? ;) You select a script and I do it. If I do it you owe me a pizza and make me coffee each morning for a week, otherwise I will for you.

J: Hm.. Sounds good. What about ajax tabs?

M: No problem :) How much time?

J: 10 minutes?

M: Great!

As you see, resolution to the matter involved a little fun and a wager (pizza).

Experience took its part – Justin lost. We recorded the process for proof ;) and now we’re posting it with a quick tutorial. You can watch Mike “cook” the code in the time-lapse YouTube video below, and you can download his AJAX tabs here.

That’s it ;) Click the Demo button to see it in action.

Here’s the code he created:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="">
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
     <title>Tabs Demo</title>
     <link rel="stylesheet" href="./main.css" />
     <script type="text/javascript" src="./jquery-1.3.2.min.js"></script>
     <script type="text/javascript" src="./main.js"></script>
     <div id="page">
          <ul id="tabs">
               <li><a href="./tabs/tab-1.html">Tab 1</a></li>
               <li><a href="./tabs/tab-2.html">Tab 2</a></li>
               <li><a href="./tabs/tab-3.html">Tab 3</a></li>
          <div id="tabs-container">
               Loading. Please Wait...


     margin: 20px;
     padding: 20px;
     font-family: "Lucida Grande", Arial, Helvetica, sans-serif;
     font-size: 12px;
     color: #333333;
#page {
     margin: auto;
     width: 1000px;
UL.mytabs {
     position: relative;
     z-index: 2;
UL.mytabs, UL.mytabs LI {
     margin: 0;
     padding: 0;
     list-style: none;
     float: left;
UL.mytabs LI { padding: 0 5px; }
UL.mytabs LI A {
     float: left;
     padding: 7px;
     border: 1px solid #CCCCCC;
     border-bottom: 1px solid #E0E0E0;
     background: #F0F0F0;
     text-decoration: none;
     color: #333333;
     height: 22px;
UL.mytabs LI A:HOVER, UL.mytabs LI.current A {
     background: #FFFFFF;
UL.mytabs LI.current A {
     font-weight: bold;
     font-size: 14px;
     border-bottom: 1px solid #FFFFFF;
.mytabs-container {
     position: relative;
     z-index: 1;
     clear: both;
     border: 1px solid #E0E0E0;
     padding: 10px;
     top: -1px;


var containerId = '#tabs-container';

var tabsId = '#tabs';

     // Preload tab on page load
     if($(tabsId + ' LI.current A').length > 0){
          loadTab($(tabsId + ' LI.current A'));

     $(tabsId + ' A').click(function(){
          if($(this).parent().hasClass('current')){ return >false; }

          $(tabsId + ' LI.current').removeClass('current');

          return >false;

function loadTab(tabObj){
     if(!tabObj || !tabObj.length){ return; }

     $(containerId).load(tabObj.attr('href'), function(){


July 25, 2012

Great code… Is there a way to make it sticky and remember the cookies on last clicked tab ?


June 6, 2012

Nice tutorial, and very cleanly done. Two comments:

(1) You’ve got a stray “>” character in the Javascript output there: it has “return >false;” instead of “return false;”.

(2) Is it possible to add the ability to link to a tab using ordinary links *within* the tab container? i.e. in the text body corresponding to Tab 1, there would be a standard text link that would take you to Tab 2?


April 20, 2012

It doesnt seem to work for me! im guessing it is because i am missing main.js ….

where can i get it?


August 31, 2011

Guess I’m missing the AJAX. All I see is JQuery.

Of course you could have done it faster with one simple java script function…


June 18, 2011

Great script!! and quite a RARE solution of what should be logical integration: Tabs and AJAX

However, it DOES NOT work with foreign characters in files loaded with AJAX.
HTML files with charset=iso-8859-1 show correctly only in Chrome.
FF and Opera mobile show infamous black rhomb with ?
and MSIE 8 “eats” chars and leave rectangles instead of foreign chars.
See it here:

There should be some solution with $.ajax dataType option
but I can’t find it. Much obliged for help.


May 18, 2011

I love, love this script but it doesn’t work on mobile Safari…any thoughts on this?


Jonathan G

April 3, 2011

For some reason, I cannot get a javascript image circulator which is based in html code and has linking to style sheets to load in a tab. be great if you could help me out, thanks :)


March 21, 2011

not working in Chrome, how am I going to fix it?

Jerry R. Reynolds

March 12, 2011

This is a great post but I need something more. If you visit my site at alimoeinc[dot]com/lab/ you can see the tabs are working fine but I cannot get the php to load. Suggestions? Would you guys be willing to create a tutorial that show how to call back php within the tabs created in this demo?


March 8, 2011

ahaan ! nice tabs -
What if u make it more robust by using history plugin for Url strcture ?Is it possible ?



February 11, 2011

I’m wondering if Brad was asking if you could link directly to a tab and have it opened – i.e. click on link and tab 2 is activated and loaded?

December 27, 2010

Hello Thomas!
At first you should check whether your website uses a different JS framework (for instance Prototype or Mootools) and if so, you may use jQuery.noConflict() method to avoid conflict between jQuery and the other framework (you may get more information about jQuery.noConflict here:
If not, please check if your header.php contains link to jQuery framework before any other scripts go.

If the above solutions did not work please ( – we will try to get more details and work out a solution for you.


December 26, 2010

Hey guys,
I’m trying to use your script with wordpress on the home.php. But I can’t get it to work, because it seems to conflict with the wordpress built-in jquery version. I even don’t get to work the compatibility mode….

December 22, 2010

Hi Gukkie!
The CSS class ‘loading’ is used only when tab’s content is loading.

Unfortunatelly you won’t be able to load data from external url because they will load only a part of HTML code, without header and footer parts. In case you would like to use external URLs you would need to use IFRAME tag and load content into it.


December 19, 2010

Hi, is it possible to have the URL displa the current page it is on?


December 17, 2010

Hi where is the “loading” class at?


November 3, 2010

Hi, David Jon!
We inspected your code and would like to confirm that your ‘mostviews.php’ script returns 404 error code (regardless its content get loaded or not), we can also see it in respose in Firebug but server still returns header with 404 code.
You may want to consider change loading direct script to a url of WordPress post, this should elimitate the issue or otherwise you need to check why your server returns 404 error code.
If you need a personal help please use contact form at our website to get in touch with one of our developers.

November 3, 2010

Hi, Velviapersky.
You’re right. We have missed it in the CSS. But if you would add a ‘loading’ class into your CSS and will assign a spinner to it for example it would display progress indicator properly.


October 24, 2010

can i know where is class ‘loading’?

David Jon

October 23, 2010

I’m having troubles using it with Wordpress. I created a file called mostviews.php to load in to my index.php template through the Ajax Tabs.
However whenever I check it in my Console, I get this warning message.

Failed to load resource: the server responded with a status of 404 (Not Found)

Eventhough the file exists, I can even view it in firebug.

If anyone can help me, please…im stuck!

July 8, 2010


You move pretty fast. I didn’t even have time to look :). I’m glad you got the tabs working. Let us know if there are any other issues.




July 7, 2010

Problem solved…


July 7, 2010

Hi Darren,

Got that fixed when I changed all the html documents to php in the tabs folder, but it won’t load any data from my sql-db into the tabs-documents?

Thanks again :)


July 7, 2010

Hi Darren,

I got the script working but when I try to change the file extentions to php it doesn’t work any more. The file loads and then it loads another page which still is an html file… Any thoughts?

I would like to use my sql-database to display information on the different tabs.


Darren – ajaxBlender

May 25, 2010


the script was actually developed and tested in Chrome, initially. please test it again. (we have been upgrading our servers and you could have possibly hit it at that time, but all should be good now.)…

thanks for the comments and help too!


February 5, 2010

Darren -

Thanks for the reply. Shortly after I posted the message, I found that it did work fine with other browsers such as FireFox and Chrome but not IE 7. I don’t have this on the web, just testing on a development server. If I can get it working on the dev server, it will then be moved to prod.

Thanks for the reply.

Darren – ajaxBlender

February 3, 2010

Brian, the extension type should not matter. It should work fine with .cfm. If you could provide us with the link to view the implementation, we will try to help fix the problem. Thanks!


February 1, 2010

Very nice but if I replace the .html pages with ColdFusion pages (ie tab-1.html replaced with tab-1.cfm), the pages will not display. Must the linked pages be in .html format?


January 26, 2010

That did the job, thank you very much for this excellent script and the help!

Darren – ajaxBlender

January 26, 2010

Alex.f, I see that the HTML files you’re trying to embed (i.e. have <html><body> tags in them. This is not needed for the implementation of the tabs. You should leave <div id=”container1″>…</div> in the file only. This should fix it. Let us know, if not. Thanks!


January 26, 2010

Hey, nice script there. I got a problem using it with Google Chrome though, the content within the tabs will look really weird, it works fine in other browser though…

Joshua Sortino

December 16, 2009

You guys should start “The National Speed Coding League” (NSCL) and advertise on ESPN (with all their channels, maybe they’ll cater one for the geeks.)

December 7, 2009

Thanks for your question, but I’m not sure I understand it fully. I assume you are not wanting to link them to a new target, as this would not technically require AJAX (or even be AJAX).

Are you asking if the content on the AJAX tabs can be crawled by the search engines? If so, then yes. The code for the AJAX tabs is SEO friendly, as it is built using unordered lists and with links to the tabs. All search engines will crawl and index the content of each tab. If this is not what you are asking, please clarify and we will try to help. Thanks!


December 5, 2009

Can you target the tabs?

Leave a comment

Your name

July 21, 2017

Spam protection. Enter result of expression you see in the box below.

1 + 5 =