Dynamic JavaScript Tabs

This is some code that I wrote to create dynamic tabs. I use it for forms where I have a lot of fields and a good percentage are "advanced" or otherwise not important enough to have visible immediately. The CSS is based on ideas from Silverorange Labs and the JS code is mine. The code is released under the MIT license which basically means you can do what you like with it as long as you don't blame me!

Basic tutorial

It's not hard to use and I've included a sample html file in the zip archive. The basics are:

<div id="tab-container">
  <div class="tab-content">
      <h1 class="tab" title="title for page 1">Page 1</h1>
      <p>This is the content of tab 1</p>
  </div>
  <div class="tab-content">
      <h1 class="tab" title="title for page 2">Page 2</h1>
      <p>This is the content of tab 2</p>
  </div>
  <div class="tab-content">
      <h1 class="tab" title="title for page 3">Page 3</h1>
      <p>This is the content of tab 3</p>
  </div>
</div>
<script type="text/javascript" src="abs.js"></script>

i.e. you need a "tab-container" and then each tab is contained in a "tab-content" and we use a "tab" to sort out the title. The nice thing is that it degrades really nicely!

Update: I've created a separate page to hold the download link

12 Responses to “Dynamic JavaScript Tabs”

  1. 1 Jimmy

    Arrgghh,

    Anyone know how to have 2 different sets of tabs on the same page.

    It would be much appreciated if someone could tell me.

  2. 2 Rob...

    Funnily enough, I received a patch for just this situation earlier this month. I haven't had a chance to look at it at all, yet so I'm just putting it up directly so that you have look at it.

    Patch to Provide Multiple Sets of Dynamic Tabs

  3. 3 emehrkay

    Is there anyway to set an active tab? lets say you wanted a form in one tab and when the user presses submit, you want them to stay on that tab.

  4. 4 Rob...

    emehrkay: the last line of tabs.js controls which tab is set to active.

    i.e. ActivateTab(0); set's the first tab as active, but ActivateTab(1); would set the second tab as the active one.

  5. 5 Jimmy

    What do I do with the patch file, there seems to be bits everywhere and loads of @'s and +'s .

    Is there some sort of program i use to interpret this file ?

  6. 6 Rob...

    Use patch (http://www.gnu.org/software/patch/patch.html)

    basically you do:
    patch < dynamic_tabs_multi.patch

    in the directory where the original dynamic tabs files are.

  7. 7 emehrkay

    Rob: how would i make it dynamic? like could i add an onclick even to the h1 tag that sets ActivateTab() to its number, incase the page is refreshed?

  8. 8 Jimmy

    what happens if I am a windows user ?

  9. 9 Jimmy

    Anychance you could run it and the re-zip it for me :)

  10. 10 Rob...

    The way we do it at work is to call ActivateTab() at the bottom of the php template like this:

    <script language="javascript">
        ActivateTab(<?php echo $tab_id ?>);
    </script>
  11. 11 emehrkay

    Rob: easy way to handle it. thanks

  12. 12 Rob...

    Jimmy: Version 1.0.2 now available here!

The views expressed in these comments are not the views of the publisher. However, we believe in the rights of others to express their legitimate views and concerns. Any legitimate complaint emailed to rob@akrabat.com will be seriously considered and the post reviewed as desirable and necessary.

Leave a Reply

Pre order