Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a bootstrap tab active?

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?

like image 864
user3766332 Avatar asked Aug 29 '26 00:08

user3766332


2 Answers

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();

});
like image 189
Brijesh Bhatt Avatar answered Aug 31 '26 14:08

Brijesh Bhatt


Try this

$("#Chart3").click(function(){
     $('li.active').removeClass('active');
    $('this').addClass('active');
});
like image 20
madalinivascu Avatar answered Aug 31 '26 16:08

madalinivascu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!