Control multiple tab-contents with one nav-tabs
Asked Answered
R

4

21

I have a regular Twitter Bootstrap 3 tab. What I want to do is to control is to control multiple
tab-content container with one nav-tabs element.

Here is an example: jsfiddle

In this example, when I change tabs, only first one is changed. I want for both containers to change, not just first one.

Thanks!

Raji answered 1/11, 2013 at 1:57 Comment(0)
P
42

Here I update your jsfiddle

I add the data-target attribute to a-elements and change ids in second tab-content

I modified this lines,

Yours:

<li class="active"><a href="#home" data-toggle="tab">C1</a></li>
<li><a href="#profile" data-toggle="tab">C2</a>  </li>

My update:

<li class="active"><a href="#home" data-target="#home, #home_else" data-toggle="tab">C1</a></li>
<li><a href="#profile" data-target="#profile, #profile_else" data-toggle="tab">C2</a>  </li>

And the second tab-content, Yours:

<div id="myTabContent" class="tab-content">
    <div class="tab-pane fade in active" id="home">
        <p>Content 1.</p>
    </div>
    <div class="tab-pane fade" id="profile">
        <p>Content 2.</p>
    </div>
</div>

My update:

<div id="myTabContent2" class="tab-content">
    <div class="tab-pane fade in active" id="home_else">
        <p>Content 1.</p>
    </div>
    <div class="tab-pane fade" id="profile_else">
        <p>Content 2.</p>
    </div>
</div>
Pathless answered 1/11, 2013 at 2:22 Comment(1)
Is there any solution for Bootstrap 4?Corrinnecorrival
M
18

You should just use a class instead. I just was working on Bootstrap 3 tabbed content inside a modal window. Was trying to get the modal window to open to the correct tab, but I needed two tabs to open, one for the modal header, and one for the content, with extra nab tabs in the footer.

I used a class and it worked. So, change <div class="tab-pane fade in active" id="home"> to <div class="tab-pane fade in active home"> and <a href="#home" data-toggle="tab"> to <a href=".home" data-toggle="tab"> ... worked for me.

Masjid answered 20/6, 2014 at 10:9 Comment(1)
This is simple and effective.Egyptian
J
0

ID must be unique.

Read Two HTML elements with same id attribute: How bad is it really?

problem with your code

Two elements with id home , profile and myTabContent.

<div id="myTabContent" class="tab-content">
    <div class="tab-pane fade in active" id="home">
        <p>Content 1.</p>
    </div>
    <div class="tab-pane fade" id="profile">
        <p>Content 2.</p>
    </div>
</div>

<hr>

<div id="myTabContent" class="tab-content">
    <div class="tab-pane fade in active" id="home">
        <p>Content 1.</p>
    </div>
    <div class="tab-pane fade" id="profile">
        <p>Content 2.</p>
    </div>
</div>

and you are giving href="#home" which will target the first element with id.Not all elements with id.

<ul id="myTab" class="nav nav-tabs">
    <li class="active"><a href="#home" data-toggle="tab">C1</a></li>
    <li><a href="#profile" data-toggle="tab">C2</a></li>
</ul>
Jamesy answered 1/11, 2013 at 2:11 Comment(1)
Hm.. yeah I know that ID are supposed to be unique, it's just that even if i change ids to classes it's still not working for me. Care to give me an example. I'm not sure what i'm doing wrong. The example above though is from documentation.Raji
T
0

"data-target" is don't work Bootstrap4.

This is Bootstrap4 version.

<!doctype html>
<html lang="ja">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
  <ul class="nav nav-tabs" id="myTab" role="tablist">
    <li class="active"><a href="#home" data-toggle="tab" data-target="#home, #home_else" aria-selected="true" id="home_tab">Home</a></li>
    <li><a href="#profile" data-toggle="tab" data-target="#profile, #profile_else" aria-selected="false" id="profile_tab">Profile</a></li>
    <li><a href="#messages" data-toggle="tab" data-target="#messages, #messages_else" aria-selected="false" id="messages_tab">Messages</a></li>
    <li><a href="#settings" data-toggle="tab" data-target="#settings, #settings_else" aria-selected="false" id="settings_tab">Settings</a></li>
  </ul>

  <div class="tab-content">
    <div class="tab-pane active" id="home" aria-labelledby="home_tab" role="tabpanel">...1</div>
    <div class="tab-pane" id="profile" aria-labelledby="profile_tab" role="tabpanel">...2</div>
    <div class="tab-pane" id="messages" aria-labelledby="messages_tab" role="tabpanel">...3</div>
    <div class="tab-pane" id="settings" aria-labelledby="settings_tab" role="tabpanel">...4</div>
  </div>
  
  <div class="tab-content">
    <div class="tab-pane active" id="home_else" aria-labelledby="home_tab">...11</div>
    <div class="tab-pane" id="profile_else" aria-labelledby="profile_tab">...22</div>
    <div class="tab-pane" id="messages_else" aria-labelledby="messages_tab">...33</div>
    <div class="tab-pane" id="settings_else" aria-labelledby="settings_tab">...44</div>
  </div>



    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    <script>
    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
      var activated_tab = e.target // activated tab
      var previous_tab = e.relatedTarget // previous tab
      if(activated_tab.id === 'home_tab') {
        $('#home_else').css('display', 'block');
        $('#profile_else').css('display', 'none');
        $('#messages_else').css('display', 'none');
        $('#settings_else').css('display', 'none');
      };
      if(activated_tab.id === 'profile_tab') {
        $('#home_else').css('display', 'none');
        $('#profile_else').css('display', 'block');
        $('#messages_else').css('display', 'none');
        $('#settings_else').css('display', 'none');
      }
      if(activated_tab.id === 'messages_tab') {
        $('#home_else').css('display', 'none');
        $('#profile_else').css('display', 'none');
        $('#messages_else').css('display', 'block');
        $('#settings_else').css('display', 'none');
      }
      if(activated_tab.id === 'settings_tab') {
        $('#home_else').css('display', 'none');
        $('#profile_else').css('display', 'none');
        $('#messages_else').css('display', 'none');
        $('#settings_else').css('display', 'block');
      }
      // 処理,,,,,
    });
  </script>
  </body>
</html>
Tarshatarshish answered 15/10, 2020 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.