Home > Blockchain >  Javascript toggle tabs only working after clicking once
Javascript toggle tabs only working after clicking once

Time:12-15

I'm still learning JavaScript. A project I'm working on, I created each tab, styled it so it all looks good and javascript to make it happen. My issue is that when I click on a tab and then click on the next tab, I can't click back to any other tag I previously have clicked

javascript html

CodePudding user response:

Can't give a full answer as you haven't provided enough detail. You should have put your code onto jsFiddle or similar.

There is no indication in your code that you are closing the tabs you have opened. For example you are adding hustler--open to your list of classes with no indication you are removing it somewhere else.

Also the if statements in your JavaScript don't do anything useful.

To be honest you should not be writing code for tab management yourself. If you're using React use React Bootstrap and https://react-bootstrap.github.io/components/tabs/

CodePudding user response:

First of all my friend, everything after the if sentences will be executed everytime the function is triggered, so i think that you should put the sentences after the if inside else like this

If (isJobsOpen) {

isJobsOpen = false;

}

else{

isJobsOpen = true;

document.body.ClassList = " class name":

}

Though I guess it's better if you make one variable refer to the open tab cause one tab will open by one time.

If that's right and one tab by time, it's better to use just one variable ex. 'isOpen', and everytime you check that variable value if it's equal to the same tab change to the default otherwise change it to the tab name.

I mean something like this:

var isOpen = '';

function openJobs(){

if (isOpen == 'openJobs'){

isOpen = ''

}

else {

isOpen = 'openJobs'

.

.

}

And sorry about the style, I'm writing you using my phone, so..

  • Related