Wednesday, April 20, 2005
by Nik Kalyani
Wednesday, April 20, 2005 7:31:38 PM (Pacific Standard Time, UTC-08:00)

There are many different solutions for creating tabbed content in HTML pages. For a number of my apps, I needed a simple, lightweight solution that was easy to use and to integrate with my apps. I came up with a simple solution and am sharing it here. Hopefully someone will find it useful.

The solution consists of a table containing the tabs and DIV’s representing the content for each tab. The tabs table has one cell for each tab with ID of “Tab_n” where n starts with 0 and increases by 1 for each tab. Add an onClick handler for each cell that calls tabClickHandler(n). Finally, create a DIV for each tab’s content and set its ID to be “Panel_n” where n starts with 0 and increases by 1 for each tab. Add a style attribute of “display: none” for all DIVs representing the tab content except for the first, which should be set to “display:block”.

<script language="javascript">
function tabClickHandler(tab)
  {
    i = -1;
    while (true)
    {
        i++;
        objTab = eval(document.getElementById("Tab_" + i));

        if (objTab)
        {
            objTab.className = (i == tab) ? "Speerio-SelectedTab" : ((i > tab) 
                                          ?
"Speerio-RightTab" : "Speerio-LeftTab");
            document.getElementById("Panel_" + i).style.display = (i == tab ? "block" : "none");
        }
        else break;
     }
  }
</script>

<style>
    .Speerio-Tabs { width:100%; color: black; font-family: Verdana,Arial,Helvetica,Sans Serif}
    .Speerio-TabLeftSpacer {width:10%;  BORDER-BOTTOM: 1px solid black}
    .Speerio-TabRightSpacer {width:10%; BORDER-BOTTOM: 1px solid black}
    .Speerio-SelectedTab { width:20%; BORDER-RIGHT: 1px solid black; BORDER-TOP: 1px solid black; BORDER-LEFT: 1px solid black}
    .Speerio-LeftTab {cursor: hand; width:20%;  BORDER-TOP: 1px solid #cccccc; BORDER-LEFT: 1px solid #cccccc; CURSOR: hand; BORDER-BOTTOM: 1px solid black; background-color: #eeeeee}
    .Speerio-RightTab {cursor: hand; width:20%;  BORDER-RIGHT: 1px solid #cccccc; BORDER-TOP: 1px solid #cccccc; BORDER-LEFT: medium none; CURSOR: hand; BORDER-BOTTOM: 1px solid black; background-color: #eeeeee}
    .Speerio-TabLabel { font-size: 8pt; font-weight: bold; color: black; font-family: Verdana,Arial,Helvetica,Sans Serif}
</style>

<table cellspacing="0" cellpadding="2" border="0" align="center" class="Speerio-Tabs">
 <tr>
  <td class="Speerio-TabLeftSpacer">&nbsp;</td>
  <td id="Tab_0" class="Speerio-SelectedTab" nowrap align="Center" valign="Middle" onClick="tabClickHandler(0)">
   <span class="Speerio-TabLabel">ABC</span></td>
  <td id="Tab_1" class="Speerio-RightTab" nowrap align="Center" valign="Middle" onClick="tabClickHandler(1)">
   <span class="Speerio-TabLabel">DEF</span></td>
  <td id="Tab_2" class="Speerio-RightTab" nowrap align="Center" valign="Middle" onClick="tabClickHandler(2)">
   <span class="Speerio-TabLabel">GHI</span></td>
  <td id="Tab_3" class="Speerio-RightTab" nowrap align="Center" valign="Middle" onClick="tabClickHandler(3)">
   <span class="Speerio-TabLabel">JKL</span></td>

  <!-- Add as many additional cells corresponding to tabs here -->
  <td class="Speerio-TabRightSpacer" align="right">&nbsp;</td>
 </tr>
</table>
  <!-– Add a DIV for each tab’s content here -->

<div id="Panel_0" style="display:block" width="100%">Contents of the first tab</div>
<div id="Panel_1" style="display:none" width="100%">
Contents of the second tab
</div>
<div id="Panel_2" style="display:none" width="100%">
Contents of the third tab
</div>
<div id="Panel_3" style="display:none" width="100%">
Contents of the fourth tab</div>


 

 

#    Comments [1] - Trackback    

ASP.Net | Javascript

Tracked by:
"loans" (loans) [Trackback]
RSS feed
Search and Links
Bling

View Nik Kalyani's profile on LinkedIn

Contact me: nik*kalyani.com (replace "*")

TechBubble
www.flickr.com
This is a Flickr badge showing public photos from techbubble. Make your own badge here.
Statistics
Total Posts: 204
This Year: 22
This Month: 0
This Week: 0
Comments: 231
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008
Nik Kalyani
Sign In
All Content © 2008, Nik Kalyani