Search code examples
jquerytabsbootstrap-4bootstrap-tabs

How can I link my button from another page to a Boostrap tab?


I know this question has been asked a bunch, but I can't seem to figure it out. I want the user to be able to click the button and jump to the correct bootstrap tab. I would prefer being able to directly link to the #whatsNew tabpanle, but I don't know if this is possible, so I have been trying to link it to the first tab "#stepOne". Any help would be appreciated, thanks!

<html lang="en">

<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://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

  <!-- Font Awesome CDN -->
  <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">




</head>

<body id="learn">

  <!-- Nav Bar -->
  <nav class="navbar navbar-nav fixed-top fixed-top-1 navbar-expand-sm navbar-light bg-light">
    <div class="container-fluid">
      <a class="navbar-brand" href="#">logo</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
      <div class="collapse navbar-collapse1 navbar-collapse justify-content-end" id="navbarNavDropdown">
        <ul class="navbar-nav">
          <li class="nav-item">
            <a class="nav-link" href="index.html">Home <span class="sr-only">(current)</span></a>
          </li>
        </ul>
      </div>
    </div>
    <!-- container -->
  </nav>
  
  <!--First set of Nav Tabs-->
  <nav class="navbar navbar-expand bg-inverse navbar-inverse fixed-top fixed-top-2">
    <div class="container-fluid">
      <div class="navbar-collapse collapse justify-content-left" id="navbar2">
        <ul class="nav nav-tabs" role="tablist">
          <li class="nav-item">
            <a class="nav-link active2" href="#overview" role="tab" data-toggle="tab">Overview</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#gettingStarted" role="tab" data-toggle="tab">Getting Started</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#whatsNew" role="tab" data-toggle="tab">What's New</a>
          </li>
        </ul>
      </div>
    </div>
  </nav>


  <!-- Tab panes -->
  <section id="subNavPanes">
    <div class="tab-content">
      <div role="tabpanel" class="tab-pane show fade active" id="overview">
        <header>

          <!-- Main jumbotron for a primary marketing message or call to action -->
          <div class="container">
            <div class="jumbotron">
              <div class="row">
              </div>
            </div>
          </div>
        </header>
        <div class="container">
          <div class="row">
          
          <!---Button I want to Link to What's New Tab-->
            <p class="text-center"><button class="btn btn-primary"><a href="#" data-toggle="tab">See all the new features</a></button></p>
          </div>
        </div>
      </div>
    </div>
  </section>

  <!-- Footer -->
  <footer>
    <p>footer</p>
  </footer>



  <!--The Nav Tab Page I'm trying to link to from the Overview Tab-->
  <div role="tabpanel" class="tab-pane fade in" id="whatsNew">
    <header>
      <div class="container">
        <div class="jumbotron">
          <div class="row">
          </div>
        </div>
      </div>
    </header>
    <section id="section6" class="d-none d-md-block">
      <div class="container-fluid">
        <div class="row">
          <div class="col-md-12 text-center bhoechie-tab-container">
            <div class="row">
              <div class="col-md-3 bhoechie-tab-menu">
                <div class="list-group .d-sm-block">
                  <a href="#stepOne" class="list-group-item active text-center">
                    <h4>Objective and quantified</h4>
                  </a>
                  <a href="#stepTwo" class="list-group-item text-center">
                    <h4>Objective and quantified</h4>
                  </a>
                  <a href="#stepThree" class="list-group-item text-center">
                    <h4>Objective and quantified</h4>
                  </a>
                  <a href="#stepFour" class="list-group-item text-center">
                    <h4>Objective and quantified</h4>
                  </a>
                  <a href="#stepFive" class="list-group-item text-center">
                    <h4>Objective and quantified</h4>
                  </a>
                  <a href="#stepSix" class="list-group-item text-center">
                    <h4>Objective and quantified</h4>
                  </a>
                </div>
              </div>
              <div class="col-md-9 bhoechie-tab text-center">
                <!-- flight section -->
                <div id="stepOne" class="bhoechie-tab-content active">

                  <img class="img-fluid " src="" alt="picture">
                  <div class="tab-text">
                    <h3>...</h3>
                    <p>...</p>
                  </div>
                </div>

                <!-- train section -->
                <div id="stepTwo" class="bhoechie-tab-content">

                  <img class="img-fluid" src="" alt="figure10">
                  <div class="tab-text">
                    <h3>...</h3>
                    <p>....</p>
                  </div>

                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  </div>


  <!-- Footer -->
  <footer id="footer">
    <p>footer...</p>
  </footer>



  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.3.1/js/mdb.min.js"></script>


</body>

</html>


Solution

  • Use an identifier on the button and assign a handler to its .click() inside which you manually trigger the desired tab's .click().

    HIH

    $('#gotonew').click(function(){
      $('a[href="#whatsNew"]').click();
      console.log($('a[href="#whatsNew"]').text() + ' click triggered');
    });
    <html lang="en">
    
    <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://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    
      <!-- Font Awesome CDN -->
      <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
    
    
    
    
    </head>
    
    <body id="learn">
    
      <!-- Nav Bar -->
      <nav class="navbar navbar-nav fixed-top fixed-top-1 navbar-expand-sm navbar-light bg-light">
        <div class="container-fluid">
          <a class="navbar-brand" href="#">logo</a>
          <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
          <div class="collapse navbar-collapse1 navbar-collapse justify-content-end" id="navbarNavDropdown">
            <ul class="navbar-nav">
              <li class="nav-item">
                <a class="nav-link" href="index.html">Home <span class="sr-only">(current)</span></a>
              </li>
            </ul>
          </div>
        </div>
        <!-- container -->
      </nav>
      
      <!--First set of Nav Tabs-->
      <nav class="navbar navbar-expand bg-inverse navbar-inverse fixed-top fixed-top-2">
        <div class="container-fluid">
          <div class="navbar-collapse collapse justify-content-left" id="navbar2">
            <ul class="nav nav-tabs" role="tablist">
              <li class="nav-item">
                <a class="nav-link active2" href="#overview" role="tab" data-toggle="tab">Overview</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#gettingStarted" role="tab" data-toggle="tab">Getting Started</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#whatsNew" role="tab" data-toggle="tab">What's New</a>
              </li>
            </ul>
          </div>
        </div>
      </nav>
    
    
      <!-- Tab panes -->
      <section id="subNavPanes">
        <div class="tab-content">
          <div role="tabpanel" class="tab-pane show fade active" id="overview">
            <header>
    
              <!-- Main jumbotron for a primary marketing message or call to action -->
              <div class="container">
                <div class="jumbotron">
                  <div class="row">
                  </div>
                </div>
              </div>
            </header>
            <div class="container">
              <div class="row">
              
              <!---Button I want to Link to What's New Tab-->
                <p class="text-center"><button id="gotonew" class="btn btn-primary"><a href="#" data-toggle="tab">See all the new features</a></button></p>
              </div>
            </div>
          </div>
        </div>
      </section>
    
      <!-- Footer -->
      <footer>
        <p>footer</p>
      </footer>
    
    
    
      <!--The Nav Tab Page I'm trying to link to from the Overview Tab-->
      <div role="tabpanel" class="tab-pane fade in" id="whatsNew">
        <header>
          <div class="container">
            <div class="jumbotron">
              <div class="row">
              </div>
            </div>
          </div>
        </header>
        <section id="section6" class="d-none d-md-block">
          <div class="container-fluid">
            <div class="row">
              <div class="col-md-12 text-center bhoechie-tab-container">
                <div class="row">
                  <div class="col-md-3 bhoechie-tab-menu">
                    <div class="list-group .d-sm-block">
                      <a href="#stepOne" class="list-group-item active text-center">
                        <h4>Objective and quantified</h4>
                      </a>
                      <a href="#stepTwo" class="list-group-item text-center">
                        <h4>Objective and quantified</h4>
                      </a>
                      <a href="#stepThree" class="list-group-item text-center">
                        <h4>Objective and quantified</h4>
                      </a>
                      <a href="#stepFour" class="list-group-item text-center">
                        <h4>Objective and quantified</h4>
                      </a>
                      <a href="#stepFive" class="list-group-item text-center">
                        <h4>Objective and quantified</h4>
                      </a>
                      <a href="#stepSix" class="list-group-item text-center">
                        <h4>Objective and quantified</h4>
                      </a>
                    </div>
                  </div>
                  <div class="col-md-9 bhoechie-tab text-center">
                    <!-- flight section -->
                    <div id="stepOne" class="bhoechie-tab-content active">
    
                      <img class="img-fluid " src="" alt="picture">
                      <div class="tab-text">
                        <h3>...</h3>
                        <p>...</p>
                      </div>
                    </div>
    
                    <!-- train section -->
                    <div id="stepTwo" class="bhoechie-tab-content">
    
                      <img class="img-fluid" src="" alt="figure10">
                      <div class="tab-text">
                        <h3>...</h3>
                        <p>....</p>
                      </div>
    
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </section>
      </div>
    
    
      <!-- Footer -->
      <footer id="footer">
        <p>footer...</p>
      </footer>
    
    
    
      <!-- jQuery first, then Popper.js, then Bootstrap JS -->
      <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    
      <script src="https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.3.1/js/mdb.min.js"></script>
    
    
    </body>
    
    </html>