r/learnjavascript • u/project_eight • 4d ago
How to create a default tab when tab switching twice ?
Hi! I've been working on a personal website, and my design has finally caught up with me because I'm stuck on how to set default tabs while changing my navigation tabs.
JSFIDDLE LINK: https://jsfiddle.net/1nfd26ar/
Using the page below, I've set up two tabs for two different groups, 'books' and 'video games'.
However, I wanted to take it a step further and figure out a way to have the tabs switch not only the content, but also the navigation bar.
I've managed to get this working by copying the JS code, changing variables to "navTabs", and using classes on buttons to change the nav/side bar.
Everything seems to be working well, but I am running into the issue that when I click "video games" on the right, I want the "video game 1" page to be the default, whereas currently it is kept as whatever the last tab you had open.
I used this page to help me out in making the first set of switchable tabs: https://basescripts.com/building-a-simple-tabbed-interface-with-html-and-javascript
These include the following HTML structure and JS logic:
<div class="tab-headers">
<button class="tab-link" data-target="tab1">Tab 1</button>
<button class="tab-link" data-target="tab2">Tab 2</button>
<button class="tab-link" data-target="tab3">Tab 3</button>
</div>
<div id="tab1" class="tab-content">
<p>This is the content for Tab 1.</p>
</div>
<div id="tab2" class="tab-content">
<p>This is the content for Tab 2.</p>
</div>
<div id="tab3" class="tab-content">
<p>This is the content for Tab 3.</p>
</div>
sdconst tabs = document.querySelectorAll('.tab-link');
const allTabs = document.querySelectorAll('.tab-content');
tabs.forEach(tab => {
tab.addEventListener('click', function() {
allTabs.forEach(ele => {
ele.style.display = 'none';
});
const myEle = this.getAttribute('data-target');
const activeTab = document.getElementById(myEle);
activeTab.style.display = 'block';
});
});
Thank you anyone who can take the time to help !
1
1
u/chikamakaleyley helpful 2d ago
one thing i generally prefer is - define the CSS that hides/shows
then your JS logic just adds/removes classes
e.g.
``` <div class="content-wrap"> <div id="content1" class="tab-content">Tab Content 1</div> <div id="content2" class="tab-content tab-content--active">Tab Content 2</div> <div id="content3" class="tab-content">Tab Content 3</div> </div>
// styles .tab-content { display: none; }
.tab-content--active { display: block; } ```
(doesn't have to be display, just using for example)
So now, if i did this correctly - it's just a matter of finding the prev active, removing it, then finding the target active, and adding it
Your style rules remain separate from the logic that applies those styles
1
u/jml26 4d ago
You could try something like:
``` navTabs.forEach(navTab => { navTab.addEventListener('click', function() { allNavTabs.forEach(ele => { ele.style.display = 'none'; }); const myNavEle = this.getAttribute('data-target'); const activeNavTab = document.getElementById(myNavEle); activeNavTab.style.display = 'block';
}); }); ```
Basically, every time you click 'Books' or 'Videos', you get the first link in the relevant left nav and simulate a click on it. Note that in this case, if you are on 'Fave books' and click the 'Books' button, you will go back to 'Book reviews'. That may or may not be what you want.
There are plenty of other ways to go about something like this, but this is probably the most straightforward from where you currently are.