I am modifying a tab
of bootstrap
to have marked the one that I need when loading the page and I am not able to do it dynamically.
On the one hand I use this function (taken from this website)
function activaTab(tab){
$('.nav-tabs a[href="' + tab + '"]').tab('show');};
With what I just need to pass the value of the link to show. The problem is that if I write directly the text works:
activaTab('anchor-1');
But if I take the value of an element of the page that contains the value #anchor-1
does not work. The last thing I have is a input hidden
with the value.
activaTab($('#enlace-1').val());
I'm pretty new to the js
so it's probably a basic error;)
Update (Feb 3, 2016)
Thank you very much for answering, I expand the code because I have not explained myself as clearly as I supposed.
Effectively I have removed the #
from the function but because all the values that happened to it already go with the #
.
The code of tab
is this:
<ul class="nav nav-tabs" id="tab1">
<li {% if tab_anchor == '#pr-val' %} class="active" {% endif %}><a data-toggle="tab" href="#pr-val">Valoración</a></li>
<li {% if tab_anchor == '#pr-sel' %} class="active" {% endif %}><a data-toggle="tab" href="#pr-sel">Selección </a></li>
<li {% if tab_anchor == '#pr-des' %} class="active" {% endif %}><a data-toggle="tab" href="#pr-des">Descarte</a></li>
</ul>
The ifs are from the Twig template manager to have the default tab that was previously loaded (when making an insert that returns to the one marked).
<div id="pr-val" class="tab-pane fade active in">
<div id="pr-sel" class="tab-pane fade">
<div id="pr-des" class="tab-pane fade">
With this I have the tab that I want marked but the lower div does not correspond. So what I do is look up the value of the active href and show the panel. When not working and to ensure that the failure did not come from there, provisionally I have included a hidden input with the value of the selected anchor that I have in the session to have the direct value through the anchor id.
<input id="anchor" type="hidden" name="anchor" value="{{ tab_anchor }}"/>
And the javascript code would be this:
$('#tab1 a').click(function (e) {
e.preventDefault()
$(this).tab('show')});
function activaTab(tab){
$('.nav-tabs a[href="' + tab + '"]').tab('show');
};
This is when I make the call through:
activaTab('#pr-des');
works, but if I do it through:
activaTab($('#anchor').val());
does not work, the variable anchor having the value "# pr-des".
As putting the id of the value "by hand" worked it occurred to me to do a switch
since that way I could call him directly.
$(document).ready(function(){
switch($('#anchor').val()) {
case '#pr-val':
activaTab('#pr-val');
break;
case '#pr-sel':
activaTab('#pr-sel');
break;
case '#pr-des':
activaTab('#pr-des');
break;
}
//activaTab('#pr-des');
});
And that's not how it works either. On the other hand, if after switch
, within that function, I will comment on:
activaTab('#pr-des');
and it works ... I have tested the cases of the switch
and enter the one that corresponds.
I have tried to do alert
of what the activaTab
function shows and in both cases it prints:
.nav-tabs a[href="#pr-des"]
Thanks for taking a look. I'm about to send the tabs
to the other neighborhood and make another type of "menu" ...