Tabs are awesome. When tabbed browsing first came out, I was loathe to pick it up, and it was only the released of the Google Chrome™ browser which converted me. Now I cannot do without it. One of the first things I wanted to make when I was learning JavaScript was a website tab system. This is what I came up with.
//the JavaScript function
xiTabCurrent=1;
function xiTab (n)
{
if (document.getElementById("tab"+n).style.display == "none")
{
document.getElementById("tab"+xiTabCurrent).style.display="none";
document.getElementById("button"+xiTabCurrent).className=document.getElementById("button"+xiTabCurrent).className.replace(" tabselected","");
document.getElementById("tab"+n).style.display="inline-block";
document.getElementById("button"+n).className+=" tabselected";
document.getElementById("button"+n).blur();
xiTabCurrent=n;
}
}
//The tab buttons
<div class="tabheader">
<input type="button" id="button1" class="tab tabselected" value="General" onclick="xiTab(1)" />
<input type="button" id="button2" class="tab" value="Extra" onclick="xiTab(2)" />
</div>
//The tab containers
<div id="tab1" class="tabcont" style="display:inline-block">
</div>
<div id="tab2" class="tabcont" style="display:none">
</div>
//The styling
.tab { border: 1px solid #9c9c9c; border-bottom: none; background-color: #eaeaea; margin: 0px 0px 0px 2px; display: inline-block; cursor: pointer; padding: 2px 5px 4px 5px; z-index: 1; position: relative; }
.tab:first-child { margin-left: 0px; }
.tab:hover { background-color: #c7c7c7; }
.tabheader { font-size: 14px; }
.tabcont { border: 1px solid #9c9c9c; background-color: #eaeaea; margin: 0px auto 0px auto; padding: 5px; width: 880px; position: relative; top: -1px; z-index: 100; }
.tabselected { background-color: #eaeaea; z-index: 900; position: relative; padding-top: 5px; }
.tabselected:hover { background-color: #eaeaea; }
From every test I have run, this solution works completely on every browser, with one small exception. When run with Opera, the tab will not blur (lose focus) after you press the button, which can be aesthetically unpleasant. However all the other browsers, including IE, work fine. The function sits at the top of the page (or in an external .js file) in a script tag. It is important that the xiTabCurrent variable is outside the function, as it sets the currently selected tab when the page loads. The tab containers should sit directly after the buttons to allow the styles to work correctly. You can see an example here.
How it works:
<input type=”button” id=”button1″ class=”tab tabselected” value=”General” onclick=”xiTab(1)” />
When the button is pressed, it sends the function a number, related to which button has been pressed. This number links to the id of the button and of the tab container, button1 and tab1 respectively. It is important that these numbers increment on each additional tab, and do not repeat. The function will then change the styling of the button (to show it is selected) and the tab (to change it from hidden to visible). It will then change the previously selected tab to not-selected (button) and hidden (tab container). Simple.