jQuery Tabs Tutorial

Tabs are easy to implement and can be built to work with your existing markup. This guide will walk through the process.


December 23, 2011

Writing the Markup

Lets start by writing our markup as if our visitor doesn't even have JavaScript enabled. Even if you do not wish to support users without JavaScript, it is still a good pattern to follow to exercise separation of concerns.

  <ul class='tabs'>
    <li><a href='#tab1'>Tab 1</a></li>
    <li><a href='#tab2'>Tab 2</a></li>
    <li><a href='#tab3'>Tab 3</a></li>
  </ul>
  <div id='tab1'>
    <p>Hi, this is the first tab.</p>
  </div>
  <div id='tab2'>
    <p>This is the 2nd tab.</p>
  </div>
  <div id='tab3'>
    <p>And this is the 3rd tab.</p>
  </div>

I used fragment identifiers (#tab1, #tab2, #tab3) for the href values in the navigation. Each element containing a tab's content is given an ID that corresponds to a fragment identifier. This way the links are semantic and continue to be functional even if the visitor has JavaScript disabled.

JavaScript provides direct access to the fragment identifier, or hash, for anchor element objects and the window.location object by using their hash property.

Writing the jQuery

Rather than describing the code, I am just going to include it with comments that explain each step.

  $('ul.tabs').each(function(){
    // For each set of tabs, we want to keep track of
    // which tab is active and it's associated content
    var $active, $content, $links = $(this).find('a');

    // If the location.hash matches one of the links, use that as the active tab.
    // If no match is found, use the first link as the initial active tab.
    $active = $($links.filter('[href="'+location.hash+'"]')[0] || $links[0]);
    $active.addClass('active');

    $content = $($active[0].hash);

    // Hide the remaining content
    $links.not($active).each(function () {
      $(this.hash).hide();
    });

    // Bind the click event handler
    $(this).on('click', 'a', function(e){
      // Make the old tab inactive.
      $active.removeClass('active');
      $content.hide();

      // Update the variables with the new link and content
      $active = $(this);
      $content = $(this.hash);

      // Make the tab active.
      $active.addClass('active');
      $content.show();

      // Prevent the anchor's default click action
      e.preventDefault();
    });
  });

See the complete document. I used jQuery's .on() method for event binding, so be sure to use jQuery 1.7 or higher.


Hey,

Follow me on Twitter and Github, that's where I'm most active these days. I welcome email (), but I'm afraid I no longer have time to answer personal requests for help regarding my plugins or posts. Thanks!