INTRODUCTION
EASY NAVIGATION
Welcome to a tutorial on how to create simple Javascript Tabs. Need to organize the information on your website a little better? Well, it is actually pretty easy to create tab navigation using just pure Javascript, Html, and CSS – This guide will walk you through the exact steps on how to create 2 different flavors of the tab navigation. Read on!
I have included a zip file with all the source code at the end of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
CONFESSION
AN HONEST DISCLOSURE
Quick, hide your wallets! I am an affiliate partner of Google, eBay, Adobe, Bluehost, Clickbank, and more. There are affiliate links and advertisements throughout this website. Whenever you buy things from the evil links that I recommend, I will make a commission. Nah. These are just things to keep the blog going, and allows me to give more good stuff to you guys - for free. So thank you if you decide to pick up my recommendations!
NAVIGATION
TABLE OF CONTENTS
Section A | Section B | Extra |
Closing |
SECTION A
INFORMATION HIDING
This first version that we are walking through is the “traditional” tab navigation. It has all the contents in one HTML file and works by toggling the visibility of the containers.
STEP 1) THE HTML
Simple Javascript Tabs
Whatever modified handbook dictates a chase. The unusable ghost warps the hypothetical product.
The connector concerns the imprisoned stamp. The chemical talks next to the raid.
The egg fumes? The servant prefaces the fan next to the hundred cabinet. A leather spins.
Above his racing chalk reverts the civilian glory. Does an Adjacent Number Ray the guideline?
A hand trips. Why does the friend dance? A player copes under a tough!
The squashed railroad intervenes in the criterion. When can a lake hesitate?
This should be pretty easy to understand. There are 2 parts to the HTML:
- First, you need to create the tabs navigation menu.
- Then, create the corresponding text blocks for each tab.
STEP 2) THE CSS
/* [NAVIGATION] */
#tab-nav {
background: #333;
}
#tab-nav span.tabnav {
display: inline-block;
padding: 20px 10px;
color: #fff;
text-decoration: none;
cursor: pointer;
}
#tab-nav span.active {
background: #a03b31;
}
/* [CONTENTS] */
#tab-contents {
padding: 15px;
}
#tab-contents div.tabtxt {
display: none;
}
#tab-contents div.active {
display: block;
}
/* [DOES NOT MATTER] */
html, body {
padding: 0;
margin: 0;
font-family: arial;
}
Well, just some CSS cosmetics for the HTML to make it look pretty. The only functional part here is probably hiding the contents with div.tabtxt
and showing it with div.active
.
STEP 3) THE JAVASCRIPT
THE SCRIPT
var tab = {
nav : null, // holds all tabs
txt : null, // holds all text containers
init : function () {
// tab.init() : init tab interface
// Get all tabs + contents
tab.nav = document.getElementById("tab-nav").getElementsByClassName("tabnav");
tab.txt = document.getElementById("tab-contents").getElementsByClassName("tabtxt");
// Error checking
if (tab.nav.length==0 || tab.txt.length==0 || tab.nav.length!=tab.txt.length) {
console.log("ERROR STARTING TABS");
} else {
// Attach onclick events to navigation tabs
for (var i=0; i
THE OVERVIEW
Function | Description |
init | Fired on window load, does a couple of things:
|
switch | Fired when the user clicks on a tab. Simply updates the selected tab, and show the text block accordingly. |
Property | Description |
nav | Holds all the HTML navigation tabs. |
txt | Holds all the HTML text blocks. |
HOW IT WORKS
Not going to bore you with a line-by-line explanation (trace through the code on your own if you are interested), but here is the gist of it:
- The
var tab
object contains all the key players that deal with the tab navigation. - When the window is fully loaded,
tab.init
will fire up. It gathers all the necessary HTML references, attaches click listeners to the tabs, and show the first one by default. - When the user clicks on a tab,
tab.switch
will fire up. It basically plays with the CSS styles to toggle which tab and content block to show. - That’s it! That simple.
SECTION B
AJAX LOADING
This second version of the tab navigation loads the content from various different HTML files instead.
STEP 1) THE HTML
Simple Javascript AJAX Tabs
Whatever modified handbook dictates a chase. The unusable ghost warps the hypothetical product.
The connector concerns the imprisoned stamp. The chemical talks next to the raid.
The egg fumes? The servant prefaces the fan next to the hundred cabinet. A leather spins.
Above his racing chalk reverts the civilian glory. Does an adjacent number ray the guideline?
A hand trips. Why does the friend dance? A player copes under a tough!
The squashed railroad intervenes in the criterion. When can a lake hesitate?
Not much of a difference in this version actually, just that we have separated the text blocks out into their own HTML files. Also, take note that we use in the navigation now, with
href
to specify which HTML file to load.
STEP 2) THE CSS
/* [NAVIGATION] */
#tab-nav {
background: #333;
}
#tab-nav a.tabnav {
display: inline-block;
padding: 20px 10px;
color: #fff;
text-decoration: none;
cursor: pointer;
}
#tab-nav a.active {
background: #a03b31;
}
/* [CONTENTS] */
#tab-contents {
padding: 15px;
}
/* [DOES NOT MATTER] */
html, body {
padding: 0;
margin: 0;
font-family: arial;
}
Yep, just some cosmetics again. Feel free to change these to fit your own theme.
STEP 3) THE JAVASCRIPT
THE SCRIPT
var tab = {
nav : null, // holds all tabs
txt : null, // holds the text container
init : function () {
// tab.init() : init tab interface
// Get all tabs
tab.nav = document.getElementById("tab-nav").getElementsByClassName("tabnav");
tab.txt = document.getElementById("tab-contents");
// Error checking
if (tab.nav.length==0) {
console.log("ERROR STARTING TABS");
} else {
// Attach onclick events to navigation tabs
for (var i=0; i
THE OVERVIEW
Function | Description |
init | Fired on window load.
|
switch | Fired when the user clicks on a tab. Simply updates the selected tab, and show the selected text block accordingly. |
load | Helper function for the switch function. Loads the selected HTML page into the text container. |
Property | Description |
nav | Holds all the HTML navigation tabs. |
txt | Holds all the HTML text container. |
HOW IT WORKS
The basics behind this AJAX version is pretty much the same… except that instead of showing/hiding text blocks, it loads them directly from HTML files with AJAX instead.
EXTRA
DOWNLOAD & MORE
That’s all for the code, and here is the download link as promised – Plus a small extra that may be useful to you.
DEMO
EXTRA – CSS ACCORDION
Looking for more ways to organize your contents? Check out my other guide:
3 Steps Simple Responsive Accordion (Pure CSS Only)
SOURCE CODE DOWNLOAD
Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
CLOSING
WHAT’S NEXT?
Thank you for reading, and we have come to the end of this guide. I hope that it has helped you to better organize the information in your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!
The post 3 Steps Simple Javascript Tabs appeared first on Code Boxx.