javascript – How to change tab(pill) through navigation button?

Question:

How to create button to change tab(pill)? I've tried it in a few ways, but with no success. In one of the cases, I got it, but it changes only to the 2nd tab, then it is as if it were without action.

$(document).ready(function() {
  $("#proximaAba").click(function() {
    var tab = $(this).closest('.tab-pane');
    $('#' + tab[0].id + ', .nav-pills li').removeClass('active');
    $('.nav-pills li a[href="#' + tab.next()[0].id + '"]').parent().addClass('active');
    tab.next().addClass('active');
  });
});
<ul class="nav nav-pills" role="tablist">
                <li role='presentation' class='active'><a href='#representante' aria-controls='representante' role='tab' data-toggle='pill'>Representante</a></li>
                <li role='presentation'><a href='#preposto' aria-controls='preposto' role='tab' data-toggle='pill'>Preposto</a></li>
                <li role='presentation'><a href='#outrasInformacoes' aria-controls='outrasInformacoes' role='tab' data-toggle='pill'>Outras Informações</a></li>
</ul>   

<div class="tab-content">
                <div role='tabpanel' class='tab-pane fade in active' id='representante'>            
                    <div class="form-group col-md-6">
                        <label for="nome_empresa" class="required">Nome da Empresa</label>
                        <input type="text" class="form-control" name="nome_empresa" id="nome_empresa" required>
                    </div>
                  <div class="form-group col-md-6">                        
                        <button type="button" class="btn btn-default btn-lg" id="proximaAba">Próximo</button>
                    </div>
                  </div>
  <div role='tabpanel' class='tab-pane fade in' id='preposto'>
                    <div class="form-group col-md-6">
                        <label for="nome_preposto" class="required">Nome - Preposto</label>
                        <input type="text" class="form-control" name="nome_preposto" id="nome_preposto" required>
                    </div>
    <div class="form-group col-md-6">                        
                        <button type="button" class="btn btn-default btn-lg" id="proximaAba">Próximo</button>
                    </div>
                </div>
  <div role='tabpanel' class='tab-pane fade in' id='outrasInformacoes'>                                        
                    <div class="form-group col-md-6">
                        <label for="id_vara_trabalho" class="required">Identificação Vara do Trabalho</label>
                        <input type="number" class="form-control" name="id_vara_trabalho" id="id_vara_trabalho" required placeholder="XXª Vara do Trabalho">
                    </div>
    <div class="modal-footer col-md-12">  
                        <button type="submit" class="btn btn-primary">Gerar Carta</button>
                    </div>
                </div>
            </div>        
  

Answer:

There were two buttons with the same id ( proximaAba ).

Added classes to simplify (the use of ID is not necessary for this to work).

Also, I removed the active class from the current tab-pane .

$(this).parents(".tab-pane").removeClass('active');

Run the code below and see the example working.

$(document).ready(function() {
  
  $(".proximaAba").click(function() {
    
    var tab = $(this).closest('.tab-pane');
    
    $('.' + tab[0].id + ', .nav-pills li').removeClass('active');
    $('.nav-pills li a[href="#' + tab.next()[0].id + '"]').parent().addClass('active');
    tab.next().addClass('active');
    
    $(this).parents(".tab-pane").removeClass('active');
    
  });
  
});
<!-- BOOTSTRAP Latest compiled and minified CSS -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>


<ul class="nav nav-pills" role="tablist">
      <li role='presentation' class='active'>
        <a href='#representante' aria-controls='representante' role='tab' data-toggle='pill'>Representante</a>
      </li>
      <li role='presentation'>
        <a href='#preposto' aria-controls='preposto' role='tab' data-toggle='pill'>Preposto</a>
      </li>
      <li role='presentation'>
         <a href='#outrasInformacoes' aria-controls='outrasInformacoes' role='tab' data-toggle='pill'>Outras Informações</a>
      </li>
</ul>   

<div class="tab-content">
    <div role='tabpanel' class='tab-pane fade in active' id='representante'>            
        <div class="form-group col-md-6">
             <label for="nome_empresa" class="required">Nome da Empresa</label>
             <input type="text" class="form-control" name="nome_empresa" id="nome_empresa" required/>
        </div>
        <div class="form-group col-md-6">                        
                  <button type="button" class="btn btn-default btn-lg proximaAba" id="proximaAba1"  >Próximo</button>
        </div>
    </div>
    <div role='tabpanel' class='tab-pane fade in' id='preposto'>
        <div class="form-group col-md-6">
            <label for="nome_preposto" class="required">Nome - Preposto</label>
            <input type="text" class="form-control" name="nome_preposto" id="nome_preposto" required/>
        </div>
        <div class="form-group col-md-6">                        
             <button type="button" class="btn btn-default btn-lg proximaAba"  id="proximaAba2">Próximo</button>
        </div>
     </div>
     <div role='tabpanel' class='tab-pane fade in' id='outrasInformacoes'>                                        
     <div class="form-group col-md-6">
         <label for="id_vara_trabalho" class="required">Identificação Vara do Trabalho</label>
         <input type="number" class="form-control" name="id_vara_trabalho" id="id_vara_trabalho" required placeholder="XXª Vara do Trabalho"/>
      </div>
      <div class="modal-footer col-md-12">  
           <button type="submit" class="btn btn-primary">Gerar Carta</button>
       </div>
    </div>
</div>
Scroll to Top