I am currently working on implementing tabs using a JavaScript function. The issue I am facing is that when I try to have tabs within one of the tabs, everything disappears every time I click on one of the inner tabs. This happens because the JavaScript function I am using sets the style.display
to none.
My goal is to ensure that when doel1
, doel2
, or doel3
is clicked inside the 'POP' tab, the style.display
of the 'POP' tab remains active.
Below is the JavaScript function I am using (sourced from w3school.com):
function openTab(evt, openTab) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(openTab).style.display = "block";
evt.currentTarget.className += " active";
}
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}
.tab button:hover {
background-color: #ddd;
}
.tab button.active {
background-color: #ccc;
}
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
<div class="tab">
<button class="tablinks" onclick="openTab(event, 'bla')">bla</button>
<button class="tablinks" onclick="openTab(event, 'blabla')">blabla</button>
<button class="tablinks" onclick="openTab(event, 'pop')">POP</button>
</div>
<div id="bla" class="tabcontent">
<h2>bla</h2>
<table>
<tr>
<td>My stuff that works :)</td>
</tr>
</table>
</div>
<div id="blabla" class="tabcontent">
<h3>blabla</h3>
<table>
<tr>
<td>My stuff that works ;)</td>
</tr>
</table>
</div>
<div id="pop" class="tabcontent">
<h1>POP</h1>
<div class="tab">
<button class="tablinks" onclick="openTab(event, 'doel1')">Doel 1</button>
<button class="tablinks" onclick="openTab(event, 'doel2')">Doel 2</button>
<button class="tablinks" onclick="openTab(event, 'doel3')">Doel 3</button>
</div>
<div id="doel1" class="tabcontent">
My stuff that doesn't show when I want :(
</div>
</div>