I am using Twitter Bootstrap for a tabular interface. When I click on a tab, I am calling an function that hides and shows corresponding divs. This is my HTML Code:
<ul class="nav nav-tabs">
<li class="active" id="Chart1"><a href="#">Chart 1</a></li>
<li id="Chart2"><a href="#">Chart 2</a></li>
<li id="Chart3"><a href="#">Chart 3</a></li>
<li id="Chart4"><a href="#">Chart 4</a></li>
</ul>
Based on that, I am using the following jquery to show and hide content:
$(document).ready(function(){
$("#pie").hide();
$("#bar").hide();
$("#Chart2").click(function(){
$("#StateWise").hide();
$("#pie").show();
});
$("#Chart3").click(function(){
$("#StateWise").hide();
$("#pie").hide();
$("#bar").show();
});
});
How can I do that on click, the active class changes to that particular tab?
You can write like this:
$("#Chart2").click(function() {
$(".active").removeClass("active");
$(this).addClass("active");
$("#StateWise").hide();
$("#pie").show();
});
$("#Chart3").click(function() {
$(".active").removeClass("active");
$(this).addClass("active");
$("#StateWise").hide();
$("#pie").hide();
$("#bar").show();
});
Try this
$("#Chart3").click(function(){
$('li.active').removeClass('active');
$('this').addClass('active');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With