0

I am trying to create an onclick event which will show/hide elements of an array however I am struggling with the showSubTabGroup() function below.

Sidenote: Eventually I would like to make this onmouseenter and onmouseleave, rather than 'click' as you see below.

I would like to be able to click on a div and show subsequent div's below as you might expect from a navigation feature.

The console is not returning any errors, however the 'click' function seems alert("CLICKED") properly.

Any help would be greatly appreciated.

Problem:

Tab.prototype.showSubTabGroup = function (tabIndex, subTabIndex) {
  this.tab[tabIndex].addEventListener('click', function () {
    alert("CLICKED");//Testing 'click' call
    for (var i = subTabIndex; i < this.subTabGroup; i++) {
      this.subTab[i].style.display = "";
    }
  });
}

    function Tab (subTabGroup) {
      this.tab = document.getElementsByClassName("tab");
      this.subTab = document.getElementsByClassName("sub-tab");
      this.subTabGroup = subTabGroup;
    }
    
    Tab.prototype.hideSubTabs = function () {
      for (var i = 0; i < this.subTab.length; i++) {
        this.subTab[i].style.display = "none";
      }
    }
    
    Tab.prototype.showSubTabGroup = function (tabIndex, subTabIndex) {
      this.tab[tabIndex].addEventListener('click', function () {
        for (var i = subTabIndex; i < this.subTabGroup; i++) {
          this.subTab[i].style.display = "";
        }
      });
    }
    
    var tab = new Tab(3);
    tab.hideSubTabs();
    tab.showSubTabGroup(0,0);
<!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>Responsive Nav</title>
    </head>
    <body>
      <!-- JQuery CDN -->
      <script src="http://code.jquery.com/jquery-3.1.0.js"></script>
    
      <div class="container">
        <div class="tab">
          <p>TAB</p>
        </div>
        <div class="sub-tab">
          <p>SUBTAB</p>
        </div>
        <div class="sub-tab">
          <p>SUBTAB</p>
        </div>
        <div class="sub-tab">
          <p>SUBTAB</p>
        </div>
      </div>
    
      <div class="container">
        <div class="tab">
          <p>TAB</p>
        </div>
        <div class="sub-tab">
          <p>SUBTAB</p>
        </div>
        <div class="sub-tab">
          <p>SUBTAB</p>
        </div>
        <div class="sub-tab">
          <p>SUBTAB</p>
        </div>
      </div>
    
      <div class="container">
        <div class="tab">
          <p>TAB</p>
        </div>
        <div>
          <div class="sub-tab">
            <p>SUBTAB</p>
          </div>
          <div class="sub-tab">
            <p>SUBTAB</p>
          </div>
          <div class="sub-tab">
            <p>SUBTAB</p>
          </div>
        </div>
      </div>
    
      <script type="text/javascript" src="tab.js"></script>
      <script type="text/javascript" src="tabEvents.js"></script>
    
    </body>
    </html>

1
  • 1
    console.log(this); Commented Oct 21, 2016 at 12:50

2 Answers 2

1

The problem of what is this inside the click. It is not your tab code, it is the reference to the element that you clicked on. You need to change that by using bind

Tab.prototype.showSubTabGroup = function (tabIndex, subTabIndex) {
  this.tab[tabIndex].addEventListener('click', (function () { 
    for (var i = subTabIndex; i < this.subTabGroup; i++) {
      this.subTab[i].style.display = "";
    }
  }).bind(this));
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is perfect! Thank you so much! :)
I'l have to look up bind(), not exactly sure how it works.
0

As I read your post, I assume hideSubTabs() is working properly. In that case I will suggest to use in showSubTabGroup():

this.subTab[i].style.display = "initial";

instead of

this.subTab[i].style.display = "";

"initial" will set to its default e.g. as "block" for div and "inline" for span

I see that you also have included jQuery. If I may ask why don't jQuery functions which will do the same with less code:

$('.tab').on('click',function() {
    $(this).closest('.container').find('.sub-tab').toggle()
})

$('.tab').click();
$('.tab')[0].click();

2 Comments

jQuery never has less code, you know there is KBs of library code that stuff is using? ;)
thats correct there are KB of library code that stuff is using, but you already included. besided of that you will probably want to slide the tabs down when opening or something else and maybe other fancy stuff. This will make you code more complex. I was just asking because why invent the wheel again?

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.