Hey Folks,
Its been long since Digimantra got these Javascript tabs in the sidebar. They are pretty sleek and light in weight. Many of my friends & visitors requested me to share the tutorial for these kind of tabs so that they can customize it and put it on their blogs/websites.
These tabs are very simple to implement. You have three parts of this tab code. The HTML part contains the skeleton of your tabs and the muscle in it. CSS part fancies the tab structure. Javascript is responsible for changing the tab content and changes the CSS for the tab which is clicked by the user. For demo, please see the tabs being on the Digimantra’s sidebar.
So let us first see what HTML part contains :
<div class="tabContainer" >
<ul class="digiTabs" id="sidebarTabs">
<li id="tab1" class="selected" onclick="tabs(this);">Tab 1</li>
<li id="tab2" onclick="tabs(this);">Tab 2</li>
<li id="tab3" onclick="tabs(this);">Tab 3</li>
</ul>
<div id="tabContent">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
</div>
<div id="tab1Content" style="display:none;">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
<div id="tab2Content" style="display:none;"><img src="http://www.digimantra.com/digimantra_ad_125_125.png" alt="digimantra logo" /></div>
<div id="tab3Content" style="display:none;">The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</div>The style information is responsible for the styling of the tabs. The important style information here is “position:relative;top:1px;”, this removes the bottom border between the tab & the content area. The width for the tabs is flexible, but for this example I have a fixed width of 400px, which can be increased/decreased as required.
<style> .tabContainer{margin:10px 0;width:400px;} .tabContainer .digiTabs{list-style:none;display:block;overflow:hidden;margin:0;padding:0px;position:relative;top:1px;} .tabContainer .digiTabs li{float:left;background-color:#46AAF7;border:1px solid #e1e1e1;padding:5px!important;cursor:pointer;border-bottom:none;margin-right:10px;font-family:verdana;font-size:.8em;font-weight:bold;color:#fff;} .tabContainer .digiTabs .selected{background-color:#fff;color:#393939;}#tabContent{padding:10px;background-color:#fff;overflow:hidden;float:left;margin-bottom:10px;border:1px solid #e1e1e1;width:93%;} </style>
Javascript function picks the content from the hidden divs and replaces it with the tab selected. It also clears the class information and changes the class to selected , which changes the tab color to white and differs the selected tab from others.
<script type="text/javascript"> function tabs(x) { var lis=document.getElementById("sidebarTabs").childNodes; //gets all the LI from the UL for(i=0;i<lis.length;i++) { lis[i].className=""; //removes the classname from all the LI } x.className="selected"; //the clicked tab gets the classname selected var res=document.getElementById("tabContent"); //the resource for the main tabContent var tab=x.id; switch(tab) //this switch case replaces the tabContent { case "tab1": res.innerHTML=document.getElementById("tab1Content").innerHTML; break; case "tab2": res.innerHTML=document.getElementById("tab2Content").innerHTML; break; case "tab3": res.innerHTML=document.getElementById("tab3Content").innerHTML; break; default: res.innerHTML=document.getElementById("tab1Content").innerHTML; break; } } </script>
Complete code
<style> .tabContainer{margin:10px 0;width:400px;} .tabContainer .digiTabs{list-style:none;display:block;overflow:hidden;margin:0;padding:0px;position:relative;top:1px;} .tabContainer .digiTabs li{float:left;background-color:#46AAF7;border:1px solid #e1e1e1;padding:5px!important;cursor:pointer;border-bottom:none;margin-right:10px;font-family:verdana;font-size:.8em;font-weight:bold;color:#fff;} .tabContainer .digiTabs .selected{background-color:#fff;color:#393939;}#tabContent{padding:10px;background-color:#fff;overflow:hidden;float:left;margin-bottom:10px;border:1px solid #e1e1e1;width:93%;} </style> <script type="text/javascript"> function tabs(x) { var lis=document.getElementById("sidebarTabs").childNodes; //gets all the LI from the UL for(i=0;i<lis.length;i++) { lis[i].className=""; //removes the classname from all the LI } x.className="selected"; //the clicked tab gets the classname selected var res=document.getElementById("tabContent"); //the resource for the main tabContent var tab=x.id; switch(tab) //this switch case replaces the tabContent { case "tab1": res.innerHTML=document.getElementById("tab1Content").innerHTML; break; case "tab2": res.innerHTML=document.getElementById("tab2Content").innerHTML; break; case "tab3": res.innerHTML=document.getElementById("tab3Content").innerHTML; break; default: res.innerHTML=document.getElementById("tab1Content").innerHTML; break; } } </script> <div class="tabContainer" > <ul class="digiTabs" id="sidebarTabs"> <li id="tab1" class="selected" onclick="tabs(this);">Tab 1</li> <li id="tab2" onclick="tabs(this);">Tab 2</li> <li id="tab3" onclick="tabs(this);">Tab 3</li> </ul> <div id="tabContent">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div> </div> <div id="tab1Content" style="display:none;">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div> <div id="tab2Content" style="display:none;"><img src="http://www.digimantra.com/digimantra_ad_125_125.png" alt="digimantra logo" /></div> <div id="tab3Content" style="display:none;">The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</div>
Hope you like the code as its simple to implement, though some people can disagree with this. These tabs can be modified to serve dynamic content using Ajax. Probably that is going to be the next tutorial on tabs.
Do let me know if you face trouble implementing these simple tabs.
Stay Digified !!
Sachin Khosla






great tutorial..
this will surely help me alot..
thanks..
that is great, just copy & paste, eh ?
yep!! i o that only
lol..
Very good tutorial.
Thanks.
If m adding more tab with this, its not working..
it means previous one works next one wont work
Ensure you have given unique ID(s) to any new tab(s) that is/are being added.
i have send you my jsp…
please do reply
i have given unique IDs , it work fine when i load my page,
but submitting page , wont work
i need to select date and then search button, on basis of data will be displayed, after this new tabs doesnt work
cant comment without looking at the code/working demo.
Hey could you please give a sample piece of how to implement this scenario…4 tabs….say i am on tab1 and on click of continue on tab1 it lands me on tab2 with tab1 hyperlink enabled and the tab3 tab4 greyed out..and if i proceed from tab2 to tab3 the tab1 tab2 hyperlink should be enabled and proceed to tab4 will enable all the prior tabs ..could you please give me guide me on how to implement this…i am learning this on my own after looking at your tutorial i couldnt resist from asking this….so please kindly let me know..