how to display next bootstrap tab on button click

Ninad picture Ninad · Sep 12, 2016 · Viewed 9.5k times · Source

I want to display next bootstrap tabs one by one on button click.Right now on button click only the second tab is displayed as i have hard coded it what should be in place of the href="#second" how to get the particular id in the href?When I first click on other tabs instead of clicking on the first tab which is active then the user must get the error message that ' the user have to click the first tab and then the other tabs' using noty plugin means disable the other tabs except for the first tab on page load .

here is my code

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Case</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script>
  $(function(){

        $('#changetabbutton').click(function(e){
            e.preventDefault();

            $('#mytabs a[href="#second"]').tab('show');
        });

    });
  </script>
</head>
<body>

<div class="container">

    <!-- Nav tabs -->
    <ul id="mytabs" class="nav nav-tabs" role="tablist">
      <li class="active">
          <a href="#first" role="tab" data-toggle="tab">
              <i class="fa fa-home"></i> First tab
          </a>
      </li>
      <li><a href="#second" role="tab" data-toggle="tab">
          <i class="fa fa-user"></i> Second tab
          </a>
      </li>
      <li>
          <a href="#third" role="tab" data-toggle="tab">
              <i class="fa fa-envelope"></i> Third tab
          </a>
      </li>
      <li>
          <a href="#fourth" role="tab" data-toggle="tab">
              <i class="fa fa-envelope"></i> Fourth tab
          </a>
      </li>
      <li>
          <a href="#fourth" role="tab" data-toggle="tab">
              <i class="fa fa-envelope"></i> Fifth tab
          </a>
      </li>
    </ul>

    <!-- Tab panes -->
    <div class="tab-content">
      <div class="tab-pane fade active in cont" id="first">
          <h2>First tab</h2>
      </div>
      <div class="tab-pane fade cont" id="second">
          <h2>Second tab</h2>
      </div>
      <div class="tab-pane fade cont" id="third">
          <h2>Third tab</h2>
      </div>
      <div class="tab-pane fade cont" id="third">
          <h2>Fourth tab</h2>
      </div>
       <div class="tab-pane fade cont" id="third">
          <h2>Fifth tab</h2>
      </div>

    </div>
     <button type="button" id="changetabbutton" class="btn btn-primary ">Set next tab</button>
</div>

</body>
</html>

Answer

Rahul Patel picture Rahul Patel · Sep 12, 2016

By using $('.nav-tabs > .active').next('li').find('a') selector you can select next tab and make it active. And if next selector not found make the first tab active.

$(function(){

  $('#changetabbutton').click(function(e){
    e.preventDefault();
    var next_tab = $('.nav-tabs > .active').next('li').find('a');
    if(next_tab.length>0){
      next_tab.trigger('click');
    }else{
      $('.nav-tabs li:eq(0) a').trigger('click');
    }
  });
});
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">

  <!-- Nav tabs -->
  <ul id="mytabs" class="nav nav-tabs" role="tablist">
    <li class="active">
      <a href="#first" role="tab" data-toggle="tab">
        <i class="fa fa-home"></i> First tab
      </a>
    </li>
    <li><a href="#second" role="tab" data-toggle="tab">
      <i class="fa fa-user"></i> Second tab
      </a>
    </li>
    <li>
      <a href="#third" role="tab" data-toggle="tab">
        <i class="fa fa-envelope"></i> Third tab
      </a>
    </li>
    <li>
      <a href="#fourth" role="tab" data-toggle="tab">
        <i class="fa fa-envelope"></i> Fourth tab
      </a>
    </li>
    <li>
      <a href="#fourth" role="tab" data-toggle="tab">
        <i class="fa fa-envelope"></i> Fifth tab
      </a>
    </li>
  </ul>

  <!-- Tab panes -->
  <div class="tab-content">
    <div class="tab-pane fade active in cont" id="first">
      <h2>First tab</h2>
    </div>
    <div class="tab-pane fade cont" id="second">
      <h2>Second tab</h2>
    </div>
    <div class="tab-pane fade cont" id="third">
      <h2>Third tab</h2>
    </div>
    <div class="tab-pane fade cont" id="third">
      <h2>Fourth tab</h2>
    </div>
    <div class="tab-pane fade cont" id="third">
      <h2>Fifth tab</h2>
    </div>

  </div>
  <button type="button" id="changetabbutton" class="btn btn-primary ">Set next tab</button>
</div>