var activeMainTab = "Previous";
var activeVerticalTab = "Content";
var activeNestedTab = {};
// Function to open main horizontal tabs
function openTab(evt, tabName) {
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(tabName).style.display = "block";
if (evt) {
evt.currentTarget.className += " active";
}
activeMainTab = tabName;
// If switching to Previous tab, show the last active vertical tab
if (tabName === "Previous") {
openVerticalTab(null, activeVerticalTab);
} else {
// For Present and Future, open the first vertical tab
var firstVerticalTab = document.getElementById(tabName).getElementsByClassName("tablinks-vertical")[0];
if (firstVerticalTab) {
firstVerticalTab.click();
}
}
}
// Function to open vertical tabs
function openVerticalTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("vertical-tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks-vertical");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
var tabContent = document.getElementById(tabName);
if (tabContent) {
tabContent.style.display = "block";
}
// If evt is null, it means we're calling this function programmatically
if (evt) {
evt.currentTarget.className += " active";
} else {
// Find the button for this tab and add the active class
var button = Array.from(tablinks).find(btn => btn.textContent.trim() === tabName);
if (button) {
button.className += " active";
}
}
activeVerticalTab = tabName;
// Open the first nested tab if it exists
var firstNestedTab = tabContent.getElementsByClassName("tablinks-nested")[0];
if (firstNestedTab) {
firstNestedTab.click();
}
}
// Function to open nested horizontal tabs
function openNestedTab(evt, tabName) {
var verticalTabContent = evt.currentTarget.closest('.vertical-tabcontent');
var i, tabcontent, tablinks;
tabcontent = verticalTabContent.getElementsByClassName("nested-tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = verticalTabContent.getElementsByClassName("tablinks-nested");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
activeNestedTab[activeVerticalTab] = tabName;
}
// Function to set initial active tabs on page load
function setInitialActiveTabs() {
// Activate Previous tab
openTab({ currentTarget: document.querySelector('.tablinks') }, "Previous");
// Activate Content tab
openVerticalTab(null, "Content");
// Activate first nested tab under Content
var firstNestedTab = document.querySelector('#Content .tablinks-nested');
if (firstNestedTab) {
firstNestedTab.click();
}
}
// Call setInitialActiveTabs when the page loads
window.onload = setInitialActiveTabs;
I’m having trouble with a tab navigation system where I need to manage multiple levels of tabs. Here’s a brief overview of my requirements:
Main Tabs: Horizontal tabs for Previous, Present, and Future.
Vertical Tabs: Tabs that appear when a main tab is selected.
Nested Tabs: Tabs within the vertical tab content. However the issue is when switching between main tabs (Present, Future), the Content vertical tab does not remain active when coming back to the Previous tab.