Below's my attempt at creating a simple tab control in JavaScript and CSS, without making use of any libraries or frameworks.
Does this look OK to you guys? Can you see any obvious pitfalls? I believe this should work on all modern browsers, supporting as far back as IE9.
<!DOCTYPE html>
<html>
<head>
<title>Tabs Demo</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<style type="text/css">
.tabs {
margin: 10px;
padding: 10px;
}
.tabs * {
margin: 0px;
padding: 0px;
}
.tabs > ul > li {
display: inline;
background-color: lightGrey;
border-bottom-style: solid;
border-bottom-color: white;
border-width: 1px;
-webkit-border-top-right-radius: 10px;
-moz-border-radius-topright: 10px;
border-top-right-radius: 10px;
}
.tabs > ul > li.selected {
display: inline;
border-bottom-color: lightGrey;
}
.tabs > ul > li:first-of-type {
-webkit-border-top-left-radius: 1em;
-moz-border-radius-topleft: 1em;
border-top-left-radius: 1em;
}
.tabs > ul > li > a {
padding: 1em;
text-decoration: none;
}
.tabs > div {
display: none;
}
.tabs > div.selected {
background-color: lightGrey;
display: block;
padding: 1em;
-webkit-border-radius: 2em;
-moz-border-radius: 2em;
border-radius: 2em;
-webkit-border-top-left-radius: 0px;
-moz-border-radius-topleft: 0px;
border-top-left-radius: 0px;
box-shadow: 10px 10px 5px #888888;
}
</style>
</head>
<body lang="en-US">
<div class="tabs">
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
<li>Six</li>
</ul>
<div>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</div>
<div>2 - two</div>
<div>3 - three</div>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</div>
<div>5</div>
<div>6</div>
</div>
<script type="text/javascript">
function indexOfElem(elem){
var i = 1; //start index at 1; not 0
//this.parentNode.querySelectorAll('li:preceding-sibling').length
//https://developer.mozilla.org/en-US/docs/Web/API/Node.nodeType
var desiredNodeType = elem.nodeType;
while((elem=elem.previousSibling)!=null) {if(elem.nodeType===desiredNodeType)i++;}
return i;
}
function selectTab(no) {
if(no.parentNode) {
no = indexOfElem(no.parentNode);
}
//update the tab's link
var e = document.querySelector(".tabs > ul > li.selected");
if(e) e.classList.remove("selected");
e = document.querySelector(".tabs > ul > li:nth-of-type(" + no + ")")
if(e) e.classList.add("selected");
//update the tab (div)
e = document.querySelector(".tabs > div.selected");
if(e) e.classList.remove("selected");
e = document.querySelector(".tabs > div:nth-of-type(" + no + ")");
if(e) e.classList.add("selected");
}
function windowOnloadPush(newOnload) {
var currentOnload = window.onload;
window.onload = function () {
if (currentOnload) {
currentOnload();
}
if (newOnload) {
newOnload();
}
};
}
function loadTabs() {
var es = document.querySelectorAll(".tabs > ul > li");
for (var i=0; i < es.length; i++) {
var a = document.createElement('a');
var href = document.createAttribute('href');
href.value = "#";
var onclick = document.createAttribute('onclick');
onclick.value = "selectTab(this);";
a.setAttributeNode(href);
a.setAttributeNode(onclick);
while (es[i].childNodes.length > 0) {
a.appendChild(es[i].childNodes[0]);
}
es[i].appendChild(a);
}
selectTab(1);
}
windowOnloadPush(function () { loadTabs(); });
</script>
</body>
</html>