Closed akera099 closed 1 year ago
Answered my own question. Here's how you can do it, considering your tabs look like this:
<body>
<li class="sidepanel-tab">
<a href="#" class="sidebar-tab-link" role="tab" data-tab-link="tab-2">
</a>
</li>
</body>
<script>
function changeTab() {
// Change "2" to the index of the tab you want to activate (starting from 1)
const tabIndexToActivate = 2;
// Find the <a> tag that corresponds to the desired tab
const tabLink = document.querySelector(`.sidebar-tab-link[data-tab-link="tab-${tabIndexToActivate}"]`);
// Dispatch a click event on the <a> tag to activate the tab
tabLink.dispatchEvent(new Event('click'));
}
</script>
Anyone knows a way to pragmatically switch which tab is displayed using JS?
I've been scratching my head on this and I can't get anything it to actually work.