Making buttons to change tab in a Nav-tabs Bootstrap type menu

Asked

Viewed 94 times

1

Right now, I have the code to follow: https://pastecode.xyz/view/3f023c9b

The following Javascript code is intended to make the buttons with the letters "Previous" and "Continue", respectively, return and advance a tab. But when you click, nothing happens.

<script type="text/javascript">
    function prox(){
        $('.nav-tabs').find('.active').next('li').find('a').trigger('click');
    }
    $('#btnA').click(function(){
        $('.nav-tabs').find('.active').prev('li').find('a').trigger('click');
    })
</script>

The buttons:

<div class="text-right">
    <button type="submit" class="btn btn-primary" onclick="prox()">Salvar e Continuar</button>
    <button type="button" class="btn btn-outline-secondary" id="btnA">Anterior</button>
</div>
  • There’s a mistake here: <button type="submit" class="btn btn-primary onclick="prox()">.... you put the onclick inside the class... would be: <button type="submit" class="btn btn-primary" onclick="prox()">... or rather, did not close the class.

  • My error typing the example here. In the code is right.

  • Okay! I’ll take a look.

1 answer

0


Change the event:

$('#btnP').onclick(function(){
 $('.nav-tabs').find('.active').prev('li').find('a').trigger('click');
})

for...

$('#btn').click(function(e){
   e.preventDefault(); // evita o submit
   $('.nav-tabs li a.active') // busca o <a> ativo
   .closest("li") // busca o <li> pai do <a> ativo
   .next(".nav-item") // seleciona o próximo <li>
   .find("a") // busca o <a> do próximo <li>
   .trigger('click'); // dispara o clique
});

Where #btn is the id button Load and continue and you need to select the link parent <a> to select the next link <a> within the next <li>.

The e.preventDefault(); prevents the page from being reloaded by submit.

  • Thank you so much for the answer. I changed the next to the previous to be able to use the back tab button as well. Initially, it was only working in the first occurrences of each button, so I realized that the reason was the fact that I was using ids, which at first recorded singular occurrences. When I switched from id to class, everything started working.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.