0

I have built a tab bar website that only uses one page I am using js to hide 3 elements and show one. When I click the links to show one and hide the others everything is messing up and 3 are shown or 2 it's random. Here is my code.

function unhide(divID, otherDivId, otherDivId, otherDivId) {
  var item = document.getElementById(divID);
  if (item) {
    item.className = (item.className == 'hidden') ? 'unhidden' : 'hidden';
  }
  document.getElementById(otherDivId).className = 'hidden';
}
.hidden {
  display: none;
}

.unhidden {
  display: block;
}
<div id="tweaked" class="hidden">
  <p>Test1</p>
  <footer class="bottom">
    <a class="tab current" href="javascript:unhide('home', 'tweaked', 'other', 'more')">Home<i class="material-icons">home</i></a>
    <a class="tab" href="javascript:unhide('tweaked', 'home', 'other', 'more')">Tweaks<i class="material-icons">view_headline</i></a>
    <a class="tab" href="javascript:unhide('other', 'home', 'tweaked', 'more')">Other<i class="material-icons">view_headline</i></a>
    <a class="tab" href="javascript:unhide('more', 'tweaked', 'other', 'more')">More<i class="material-icons">share</i></a>
  </footer>
</div>

12
  • otherDivId, otherDivId, otherDivId What do you expect that to do? Commented Aug 28, 2017 at 20:42
  • @epascarello there are 4 div's and when you unhide 1 it hides the other 3 Commented Aug 28, 2017 at 20:43
  • 1
    It is a really, really bad habit to have inline javascript (in your anchors) and a script as well. I would move everything to the script. Commented Aug 28, 2017 at 20:43
  • 2
    Your function takes 2 parameters - you're passing it 4. Commented Aug 28, 2017 at 20:44
  • Yes, but you can not use the same variable name for each. Most developers will have a collection of all the elements, loop over it and hide. Commented Aug 28, 2017 at 20:44

2 Answers 2

1

Perhaps you want something like this:

var anchors = document.querySelectorAll(".bottom .tab"),
    showHide = function(e) {
      var parent = this.parentNode;
      
      anchors.forEach(a => {
        var relatedDiv = document.getElementById(a.dataset.tab),
            aClass = a.className.trim();
        
        if (a.dataset.tab != this.dataset.tab) {
          relatedDiv.className = relatedDiv.className.replace("unhidden", "") + " hidden";
          a.className = aClass.replace("current", "");
        } else {
          relatedDiv.className = relatedDiv.className.replace("hidden", "") + " unhidden";
          a.className = aClass.replace("current", "") + " current";
        }
      });
    };

anchors.forEach(a => a.addEventListener("click", showHide));
.hidden{
    display:none;
}
.unhidden{
    display:block;
}
<div id="home" class="unhidden">
    <p>Home</p>
</div>
<div id="tweaked" class="hidden">
    <p>Tweaks</p>
</div>
<div id="other" class="hidden">
    <p>Other</p>
</div>
<div id="more" class="hidden">
    <p>More</p>
</div>
<footer class="bottom">
    <a class="tab current" href="#" data-tab="home">Home<i class="material-icons">home</i></a>
    <a class="tab" href="#" data-tab="tweaked">Tweaks<i class="material-icons">view_headline</i></a>
    <a class="tab" href="#" data-tab="other">Other<i class="material-icons">view_headline</i></a>
    <a class="tab" href="#" data-tab="more">More<i class="material-icons">share</i></a>
</footer>

Sign up to request clarification or add additional context in comments.

5 Comments

There should be a footer in each div with the corresponding "tab current"
@WillMays like that ? You really don't need to copy the footer for each div actually..
I change the footer class value based on what div it is
The js is not working its not showing or hiding anything just staying on home
@WillMays check update. Check the classes of the links.
0

Have you tried item.classList ? Element.classList | MDN

You could try something like this:

if(item.classList.contains("hidden")){
    item.classList.remove("hidden");
    item.classList.add("unhidden");
}else{
    item.classList.add("hidden");
    item.classList.remove("unhidden");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.