Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing active Bootstrap 3 tab using jQuery

I was experimenting with programatically changing tabs using jQuery. I tried to implement the solution given by @MasterAM here. However, the Chrome console shows the error Uncaught TypeError: undefined is not a function

Here is my HTML:

<div id="click-me-div">Click Me</div>

 <ul class="nav nav-tabs">
    <li class="active"><a href="#fruits" data-toggle="tab">I like Fruits</a></li>
    <li><a href="#veggies" data-toggle="tab">I like Veggies Too</a></li>
    <li><a href="#samosas" data-toggle="tab">But Samosa's Rock</a></li>
</ul>

<div class="tab-content">
    <div class="tab-pane active" id="fruits">Apple, Kiwi, Watermellon</div>
    <div class="tab-pane" id="veggies">Kale, Spinach, Pepper</div>
    <div class="tab-pane" id="samosas">Awesome, Spine Tingling, Explosive</div>
</div>

And here is the jQuery:

$(document).ready(function() {
    $("#click-me-div").click(function() {
        alert('yes, the click actually happened');
        ('.nav-tabs a[href="#samosas"]').tab('show');
    });
});

The desired outcome is that, if I click "Click Me", the active tab should change to the "samosas" tab.

What am I doing wrong?

like image 653
Dirty Penguin Avatar asked Jun 21 '14 17:06

Dirty Penguin


People also ask

How do I change the active tab in bootstrap?

To make the tabs toggleable, add the data-toggle="tab" attribute to each link. Then add a . tab-pane class with a unique ID for every tab and wrap them inside a <div> element with class . tab-content .

How do you make a bootstrap tab active using jquery?

$(document). ready(function(){ activaTab('aaa'); }); function activaTab(tab){ $('. tab-pane a[href="#' + tab + '"]'). tab('show'); };

How do you keep the current tab active on page reload in Bootstrap 3?

Answer: Use the HTML5 localStorage Object In Bootstrap, if you refresh the page the tab is reset to default setting. However, you can use the HTML5 localStorage object to save some parameter for the current tab locally in the browser and get it back to make the last active tab selected on page reload.

How do you make a tab active by default?

Just add the class to your html. This will make the first tab active by default.


2 Answers

You left out the jQuery function

$(document).ready(function() {     $("#click-me-div").click(function() {         alert('yes, the click actually happened');         $('.nav-tabs a[href="#samosas"]').tab('show');     }); }); 

Fiddle

like image 91
Motti Horesh Avatar answered Sep 22 '22 21:09

Motti Horesh


Try this code

You can use onclick() function and path id name of tab.

<a onclick='ActivTab("Security")' class="nav-link" data-toggle="tab" href="#Security" aria-controls="profile" role="tab" id='Security'>Security</a>


    <script>
         $(document).ready(function(){
             ActivTab('Security');
         });
         function ActivTab(id){
//           $('.nav-tabs a[href="#' + id + '"]').tab('show');
             $('.nav-tabs a[href="#' + id + '"]').trigger('click');
         };
     </script>
like image 43
Ayman Elshehawy Avatar answered Sep 20 '22 21:09

Ayman Elshehawy