jquerybootstrap-tabs

how to display next bootstrap tab on button click


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>

Solution

  •     $('#changetabbutton').click(function(e){
            e.preventDefault();
            var next = $('#mytabs li.active').next()
            next.length?
              next.find('a').click():
              $('#mytabs li a')[0].click();
        });
    

    <!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();
                var next = $('#mytabs li.active').next()
                next.length?
                  next.find('a').click():
                  $('#mytabs li a')[0].click();
            });
    
        });
      </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>