This is my HTML with CSS and JavaScript all in one place.
JavaScript
var links = document.getElementsByTagName("a"); //get the links
var len = links.length;
for(var i = 0; i<len; i++) {
links[i].onclick = handleClick; // add onclick handler
}
function handleClick(e){
var target = e.target;
var id = target.id + "content";
document.getElementById(id).style.zIndex = 10;
}
HTML
<div id="tabbed">
<a href="#" id="tabe1">Tabe1</a>
<div class="section" id="tabe1content">
<div>
<p> content1 </p>
</div>
</div>
<a href="#" id="tabe2">Tabe2</a>
<div class="section" id="tabe2content">
<div>
<p> content2 </p>
</div>
</div>
<a href="#" id="tabe3">Tabe3</a>
<div class="section" id="tabe3content">
<div>
<p> content3 </p>
</div>
</div>
</div>
CSS
.section{
position:absolute;
float:left;
width:500px;
background-color:gray;
left:0;
top:0;
height:300px;
margin-top:30px;
}
#tabbed{
position:relative;
}
a {
margin-right:10px;
float:left;
display:block;
}
After testing, I noticed that it only works once. When clicking on table 1 for the second time, it still shows table 3 instead. I'm looking for a solution to fix this issue. Is there a better way to achieve the desired functionality?