2

In a previous question, I asked how to achieve something like this:

Where clicking any tab would reveal the contents of said tab and keep those of other tabs hidden. @BradleyCoupland suggested the following code:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div style="text-align: justify">[Introduction text]</div>
<br>
<br>
<ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" href="#Vspoiler" id="defaultOpen" onclick="openPage('Vspoiler')">Vulgate version</a></li>
    <li><a data-toggle="tab" href="#Cspoiler" onclick="openPage('Cspoiler')">Campbell version</a></li>
</ul>
<div id="Vspoiler" class="tab-pane tabcontent active">
    <br>
    <table>
        <tbody>
            <tr>
                <td>
                    <div class="column" style="padding-left: 80px">
                        [Contents of col 1 of tab 1]
                    </div>
                </td>
                <td>
                    <div class="column" style="padding-left: 80px">
                        [Contents of col 2 of tab 2]
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</div>
<div id="Cspoiler" class="tab-pane tabcontent">
    <br>
    <table>
        <tbody>
            <tr>
                <td>
                    <div class="column" style="padding-left: 80px">
                        [Contents of col 1 of tab 2]
                    </div>
                </td>
                <td>
                    <div class="column" style="padding-left: 80px">
                        [Contents of col 2 of tab 2]
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</div>

<script>
function openPage(pageName) {
    var i, tabcontent;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    document.getElementById(pageName).style.display = "block";  
}
document.getElementById("defaultOpen").click();
</script>

And it worked like a charm. But suppose I now want to do multiple independent sets of tabs, like this:

enter image description here

I tried just copy-pasting the <ul> part twice, and the result was that the second set of tabs showed nothing on loading, and clicking on a tab in any tab set would do the right thing in its set, but hide all the tabs of the other set as well. For example, clicking on "weird-meter older reconstruction" would show what you see in the image for that tab, but hide the contents of the "P.Oxy. version" tab below. Simplifying the code above to a minimal example:

<div style="text-align: justify">Intro</div>
<br>
<br>
<ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" href="#Vspoiler" id="defaultOpen" onclick="openPage('Vspoiler')">Vulgate version</a></li>
    <li><a data-toggle="tab" href="#Cspoiler" onclick="openPage('Cspoiler')">Campbell version</a></li>
</ul>
<div id="Vspoiler" class="tab-pane tabcontent"><br>
    Pefanthi
</div>
<div id="Cspoiler" class="tab-pane tabcontent"><br>
    Abanthi
</div>
<br>
<br>
<ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" href="#Vspoiler2" id="defaultOpen" onclick="openPage('Vspoiler2')">Vulgate version</a></li>
    <li><a data-toggle="tab" href="#Cspoiler2" onclick="openPage('Cspoiler2')">Campbell version</a></li>
</ul>
<div id="Vspoiler2" class="tab-pane tabcontent"><br>
    Pefanthi
</div>
<div id="Cspoiler2" class="tab-pane tabcontent"><br>
    Abanthi
</div>
<br>
<br>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>   

<script>
function openPage(pageName) {
    var i, tabcontent;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    document.getElementById(pageName).style.display = "block";  

}
document.getElementById("defaultOpen").click();
</script>

Which loads to this:

enter image description here

And where clicking the lower "Vulgate version" tab produces this:

enter image description here

Now the obvious solution to this problem is to duplicate the script, by making an otherwise identical function openPage2 which appends a 2 to everything it works with, and then use openPage2, tabcontent2 and defaultOpen2 for the second set of tabs and the no-2 versions for the first one. Now suppose you have more than 2 sets. Say you have 10. 10 scripts? Surely there must be a less clumsy way of fixing this problem? I was thinking maybe one could set a variable to store the number of tabs and then make a script with a for loop that would result in running all the openPage<x> scripts at the right time by writing a single script, but I'm not sure how to go about this.

Any suggestions?

Community
  • 1
  • 1
MickG
  • 3,216
  • 2
  • 14
  • 22

1 Answers1

0

You don't need to use javascript to close/toggle the nav-tabs, bootstrap has this functionality built in. The reason why it's not working out of the box is because there are a few things missing in the html. You need to wrap all the tab content inside a div with class="tab-content" as shown below:

<ul class="nav nav-tabs">
  <!-- add data-toggle attribute to the anchors -->
  <li class="active"><a data-toggle="tab" href="#home">Home</a></li>
  <li><a data-toggle="tab" href="#menu1">Menu 1</a></li>
  <li><a data-toggle="tab" href="#menu2">Menu 2</a></li>
</ul>
<div class="tab-content"> <!-- wrapper for all tab content -->
  <div id="home" class="tab-pane fade in active">
    <h3>HOME</h3>
    <p>Some content.</p>
  </div>
  <div id="menu1" class="tab-pane fade">
    <h3>Menu 1</h3>
    <p>Some content in menu 1.</p>
  </div>
  <div id="menu2" class="tab-pane fade">
    <h3>Menu 2</h3>
    <p>Some content in menu 2.</p>
  </div>
</div>

Here is a live demo stacking two of the snippets above:
https://www.bootply.com/WuuHKy9jwR

StaticBeagle
  • 5,070
  • 2
  • 23
  • 34