Search code examples
javascriptjqueryajaxtwitter-bootstrapbootstrap-tabs

How to set active tab after fetching the view from an ajax call?


I am trying to set active tab for bootstrap 3 tabs after fetching the view from an ajax call but Its not working.

The view is something like this:

<div class="portlet light ">
    <div class="portlet-title tabbable-line">
        <div class="caption">
            <i class="icon-globe font-dark hide"></i>
            <span class="caption-subject font-dark bold uppercase">TODAY</span>
        </div>
        <ul id="tab" class="nav nav-tabs">
            <li class="active">
                <a href="#tab_1_1" class="active" data-toggle="tab"> Collections </a>
            </li>
            <li>
                <a href="#tab_1_2" data-toggle="tab"> Activities </a>
            </li>
        </ul>
    </div>
    <div class="portlet-body">
        <!--BEGIN TABS-->
        <div class="tab-content">
            <div class="tab-pane active" id="tab_1_1">

And here is the script:

<script type="text/javascript">
function load_page() 
{


    $.ajax(
        {
        url: 'notification_table/',
        type: 'GET',
        dataType: 'html',
        }).done(
                function(data)
                {
                $('#home_view').html(data);
                }
                );

    $('.nav-tabs a[href="#tab_1_2"]').tab('show');
}

 window.onload = function () 
 {
    load_page();
    setInterval(function () {
    load_page(); }, 5000);
 }
</script>

As you can see that I tried doing $('.nav-tabs a[href="#tab_1_2"]').tab('show'); and I also tried $('#tab a[href="#tab_1_2"]').tab('show'); but both are not working.

What am I doing wrong here?


Solution

  • Try to use class="active" only in li not in anchor tag, almost everything right.

     <li class="active">
         <a href="#tab_1_1" data-toggle="tab"> Collections </a>
     </li>