raold raold - 7 months ago 15
HTML Question

JQuery, Bootstrap navs - how do I add prev/next buttons?

I am using bootstrap navs for my page and I want to add next and prev buttons to my tab content.

HTML

<ul class="nav nav-tabs nav-justified">
<li class="active"><a href="#overview">Overview</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#randomtab">Randomtab</a></li>
</ul>
<div class="tab-content">
<div id="overview" class="tab-pane fade in active">

<a href="#contact" class="btn btn-info pull-right">Next</a>

</div>
<div id="contact" class="tab-pane fade in active">
</div>
<div id="randomtab" class="tab-pane fade in active">
</div>
</div>


Javascript

$(document).ready(function() {
$(".nav-tabs a").click(function() {
$(this).tab('show');
});
$('.nav-tabs a').on('shown.bs.tab', function(event) {
var x = $(event.target).text(); // active tab
var y = $(event.relatedTarget).text(); // previous tab
$(".act span").text(x);
$(".prev span").text(y);
});
});


I tried to add this JavaScript code but I don’t know how to write
a[href=(this).href]


$(".tab-pane a").click(function() {
$('a[href="(this.href)"]').tab('show'); //this row is wrong
});
$('.tab-pane a').on('shown.bs.tab', function(event) {
var x = $(event.target).text(); // active tab
var y = $(event.relatedTarget).text(); // previous tab
$(".act span").text(x);
$(".prev span").text(y);
});


This is my solution, maybe there is a better one but I don’t know...Thank you.

PS: Please leave comment below if you don’t understand my question, so I can edit this post.

EDIT

This is the working solution for bootstrap navs:

thanks to Arun P Johny

<ul class="nav nav-tabs nav-justified">
<li class="active"><a href="#overview">Overview</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#randomtab">Randomtab</a></li>
</ul>
<div class="tab-content">
<div id="overview" class="tab-pane fade in active">

<a href="#contact" class="btn btn-info pull-right">Next</a>

</div>
<div id="contact" class="tab-pane fade in active">

<a href="#overview" class="btn btn-info pull-left">Prev</a>

<a href="#randomtab" class="btn btn-info pull-right">Next</a>

</div>
<div id="randomtab" class="tab-pane fade in active">

<a href="#contact" class="btn btn-info pull-left">Prev</a>

</div>
</div>




$(document).ready(function() {
$(".nav-tabs a").click(function() {
$(this).tab('show');
});
$('.nav-tabs a').on('shown.bs.tab', function(event) {
var x = $(event.target).text(); // active tab
var y = $(event.relatedTarget).text(); // previous tab
$(".act span").text(x);
$(".prev span").text(y);
});
$(".tab-pane a").click(function() {
$('a[href="' + $(this).attr('href') + '"]').tab('show');
});
$('.nav-tabs a').on('shown.bs.tab', function(event) {
var x = $(event.target).text(); // active tab
var y = $(event.relatedTarget).text(); // previous tab
$(".act span").text(x);
$(".prev span").text(y);
});
});

Answer

I think the problem is href property will have absolute path so try

$('a[href="'+ $(this).attr('href')+'"]').tab('show');