Anchor for Bootstrap tabs

Asked

Viewed 635 times

1

Hi, I’m doing a question and answer panel, where I use the bootstrap tabs to better organize the questions. I wanted every time the user answered a question, jumped to the next one, but I can’t do it as an anchor, I searched the internet, but I only found it with URL.

Follow a picture: Imagem do sistema

And when the person clicks finish I send a confirm asking if he is sure he wants to finish. And he wanted that if the user did not confirm, return to the tab that he was on, but otherwise he would go to the tab "finish". I just wanted the way to create this anchor for tabs without having the need for the user to click each one...

Anyone who can help, I’d like to thank!

2 answers

0

Follows the codes:

<li><a data-toggle="tab" id="botaoFinalizar" onclick="confirmarEnvioQuestionario();">Finalizar</a></li>

The one on top is the Button to finish (I didn’t put the href="#finish" because it does it after the function and everything).

<li <?php echo ($r == 1) ? "class='active'" : "" ?>><a data-toggle="tab" href="#pergunta-<?php echo $r ?>"><?php echo $r ?></a></li>

Here in the case is the loop that I do to show the questions, but so far there is no problem, it works normal. The problem is that it does not anchor and also nothing appears on the console.

function confirmarEnvioQuestionario(){
//VALIDA PARA VER SE PREENCHEU TODOS OS CAMPOS
//SE SIM, PEDE PARA CONFIRMAR
if(validaCampos()){
    var c = confirm("Tem certeza que quer finalizar o questionário?");
}
else{
    alert("Responda todas as questões primeiro!");
    return false;
}
if(c == true){
    $('#finalizar').tab('show');
    carregaResultadoQuestionario();
}
else{
    $('#pergunta-1').tab('show');
}

}

And before you ask, yes, all the functions that are in this script are working and the tabs are with the corresponding id’s.

  • Try to remove the data-toggle="tab" of the element <a> on the finish button, and put a href="#".

  • I think the problem is not with the button, I tried the way you said but still not enough.

0

To change the tab via JS you can use the following command:

$('#id_tab').tab('show');

If you want to display the hash in the URL when changing tabs you can add the following function in your JS file:

$(function(){
  var hash = window.location.hash;
  hash && $('ul.nav a[href="' + hash + '"]').tab('show');

 $('.nav-tabs a').click(function (e) {
    $(this).tab('show');
    var scrollmem = $('body').scrollTop() || $('html').scrollTop();
    window.location.hash = this.hash;
    $('html,body').scrollTop(scrollmem);
  });
});

Source:https://stackoverflow.com/questions/12131273/twitter-bootstrap-tabs-url-doesnt-change

  • Thanks for the answer. But I don’t know what’s going on, because $('#id_tab'). tab('show'); it’s not working...

  • How are you doing the trigger to open the tab? $("#id_element").click(function(){}), it is important to remember that this will not work for dynamic elements, so you would have to use $(document).on( 'click' , '#id_event', function(){});. In any case it would be better if you post the code so we can see what’s going on.

  • When the person clicks to finish, I run a function to confirm if they really want to finish. I use a confirm for that. If it’s true, I want you to go to the end tab, if it’s not, I want you to go back to question number 1.

Browser other questions tagged

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