.tabbed ul {
list-style:none;
margin:0;
padding:0;
}
.tabbed ul li {
float:left;
position:relative;
background-color:#fff;
margin-bottom:-1px;
}
.tabbed ul li a {
display:block;
padding:3px 5px;
border:1px solid #ccc;
border-bottom:0;
color:green;
text-decoration:none;
}
.tabbed ul li.active a {
border-bottom:1px solid #fff;
}
.tabbed > div {
border:1px solid #ccc;
clear:both;
padding:10px;
}
.tabbed > div {
display:none;
}
.tabbed > div:first-of-type {
display:block;
}
JavaScript
// Build some basic UI tabs
//
// Desired behavior:
//
// When user clicks a tab
// that tab becomes marked as "active"
// any other active tabs are no longer active
// the related div (by id) is displayed
// any other unrelated divs are hidden
//
// Hint:
// You'll use the "click" event, and delegation is best
var tab1 = document.getElementById("tab1");
var tab2 = document.getElementById("tab2");
var tab3 = document.getElementById("tab3");
tab1.onclick = function (e) {
console.log(e); // the event
console.log(this); // refers to element
tab1.className="active";
tab2.className="";
tab3.className="";
return false;
}
tab2.onclick = function (e) {
console.log(e); // the event
console.log(this); // refers to element
tab1.className="";
tab2.className="active";
tab3.className="";
return false;
}
tab3.onclick = function (e) {
console.log(e); // the event
console.log(this); // refers to element
tab1.className="";
tab2.className="";
tab3.className="active";
return false;
}
tab1.addEventListener('click',
function (e) {
console.log("Tab1 clicked", e);
},
false // use capture method
);
tab2.addEventListener('click',
function (e) {
console.log("Tab2 clicked", e);
},
false // use capture method
);
tab3.addEventListener('click',
function (e) {
console.log("Tab3 clicked", e);
},
false // use capture method
);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.