Show Bootstrap tab loaded with Ajax doesn't work on first click


dirty encoder

I am trying to load the content of a given tab using Ajax.

I'm rendering the first tab active on the server, and the other tabs are active and loaded using Ajax.

The problem I've isolated (without Ajax) is here .

HTML:

<h1>Hello, tabs!</h1>

<div>
    <ul class="nav nav-tabs" role="tablist">
        <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a>

        </li>
        <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a>

        </li>
        <li role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">Messages</a>

        </li>
        <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a>

        </li>
    </ul>
    <div class="tab-content">
        <div role="tabpanel" class="tab-pane active" id="home">...</div>
    </div>
</div>

Javascript:

$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
    e.preventDefault();

    var ref = $(this).attr('href').replace('#', '');

    var html = '<div role="tabpanel" class="tab-pane" id="' + ref + '">Brace yourself SW7 is coming. ' + ref + '</div>';

    var tabContent = $('.tab-content');

    if (!tabContent.find('.tab-pane#' + ref).length) {
        tabContent.append(html);
    }

    tabContent.find('.tab-pane#' + ref).tab('show');
});

The Home tab is rendered active. For example, click on the Profiles tab and it will add the content but not activate the tab. Now if you click the "Home" tab, and then the "Profiles" tab again, it will work fine.

Am I doing something wrong or is this "expected" behavior?

Tony Hinkle

The problem is that the first time the tab is clicked, the content DIV doesn't exist, so when the Bootstrap code is executed, the DIV that needs to be activated cannot be found. On the second click, the DIV exists, so Bootstrap will find it. This can be fixed by hiding all tab content DIVs at the end of the script and then showing the specified tab.

$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
    e.preventDefault();

    var ref = $(this).attr('href').replace('#', '');
    var tabContent = $('.tab-content');

    if (!$('#' + ref).length) {
        // Execute the AJAX here to get the tab content
        var html = '<div role="tabpanel" class="tab-pane" id="' + ref + '">Brace yourself SW7 is coming. ' + ref + '</div>';
        tabContent.append(html);
    }

    tabContent.find('.tab-pane').hide();
    tabContent.find('#' + ref).show();
});

I'm sure this can be optimized further, but I'm running out of time right now and can use...

Related


Show Bootstrap tab loaded with Ajax on first click doesn't work

dirty encoder I am trying to load the content of a given tab using Ajax. I'm rendering the first tab active on the server, and the other tabs are active and loaded using Ajax. The problem I've isolated (without Ajax) is here . HTML: <h1>Hello, tabs!</h1> <div

.click() doesn't work with ajax loaded content

good guy I have a datatable that fetches data from my server using AJAX. This code adds data to the datatable and also adds some buttons to it var jsdata = JSON.parse(data); for (var i = 0; i < jsdata.length; i++) {

Bootstrap tab click preventDefault doesn't work

Stacey I'm using bootstrap 3.3 and following the tabs in the documentation. The simplest code given in the documentation doesn't work for me and I can't figure out why. $('#mainTabs a').click(function (e) { e.preventDefault() console.lo

Bootstrap tab click preventDefault doesn't work

Stacey I'm using bootstrap 3.3 and following the tabs in the documentation. The simplest code given in the documentation doesn't work for me and I can't figure out why. $('#mainTabs a').click(function (e) { e.preventDefault() console.lo

Bootstrap tab click preventDefault doesn't work

Stacey I'm using bootstrap 3.3 and following the tabs in the documentation. The simplest code given in the documentation doesn't work for me and I can't figure out why. $('#mainTabs a').click(function (e) { e.preventDefault() console.lo

Bootstrap tab click preventDefault doesn't work

Stacey I'm using bootstrap 3.3 and following the tabs in the documentation. The simplest code given in the documentation doesn't work for me and I can't figure out why. $('#mainTabs a').click(function (e) { e.preventDefault() console.lo

Bootstrap tab click preventDefault doesn't work

Stacey I'm using bootstrap 3.3 and following the tabs in the documentation. The simplest code given in the documentation doesn't work for me and I can't figure out why. $('#mainTabs a').click(function (e) { e.preventDefault() console.lo

JQuery Click handler doesn't work after Ajax page is loaded

Adam 78 I have an ASP MVC application in which I have a partial view that is loaded via ajax every time a new annotation is added. However, after the ajax page loads, the jQuery click handler doesn't work anymore. <div id="comments"> // Jquery links don't

Bootstrap toggle doesn't work after ajax loaded

micif I am using ajax load to get some content on a page. I am using bootstrap 3 and bootstrap switch. The bootstrap 3 content works fine when loading the content (you can clearly see the panel panel-primary). But the bootloader content doesn't get loaded (you

Bootstrap toggle doesn't work after ajax loaded

micif I am using ajax load to get some content on a page. I am using bootstrap 3 and bootstrap switch. The bootstrap 3 content works fine when loading the content (you can clearly see the panel panel-primary). But the bootloader content doesn't get loaded (you

Bootstrap .tab('show') doesn't work for my code

Debiprasad: Here is my code on JSFiddle $('button').click(function() { $('#passive_order_categories').tab('show'); }); <script type="text/javascript" src="//code.jquery.com/jquery-2.1.0.js"></script> <script type="text/javascript" src="//maxcdn.bootstrapcd

Bootstrap .tab('show') doesn't work for my code

Debiprasad: Here is my code on JSFiddle $('button').click(function() { $('#passive_order_categories').tab('show'); }); <script type="text/javascript" src="//code.jquery.com/jquery-2.1.0.js"></script> <script type="text/javascript" src="//maxcdn.bootstrapcd

Bootstrap .tab('show') doesn't work for my code

Debiprasad: Here is my code on JSFiddle $('button').click(function() { $('#passive_order_categories').tab('show'); }); <script type="text/javascript" src="//code.jquery.com/jquery-2.1.0.js"></script> <script type="text/javascript" src="//maxcdn.bootstrapcd

Tab click doesn't work

rookie I tried following this demo https://www.youtube.com/watch?v=PP3x4crHBog but since I already named other tags I can't name the same thing, so I made some changes to the variable names and Using the skins and all the formatting, but whenever I click the l

Tab click doesn't work

rookie I tried to follow this demo https://www.youtube.com/watch?v=PP3x4crHBog but couldn't name the same since I already named other tags, so I made some changes to the variable names and Using the skins and all the formatting, but whenever I click the login

show/hide script doesn't work on first click but works after

Doug Williams I'm trying to use this script I pulled from the internet to show/hide the "window" div by clicking a button in the "row 1" div. The first click nothing happens, but after that it works fine. Any help getting it to work on the first click would be

Bootstrap dropdown doesn't work after first submit ajax form

Brian Powell I have a dropdown menu on my page manager.php, here. sorry, formatting-bootstrap:: <!-- Bootstrap --> <script type="text/javascript" src="https://netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> <link rel="stylesheet" h

Bootstrap dropdown doesn't work after first submit ajax form

Brian Powell I have a dropdown menu on my page manager.php, here. sorry, formatting-bootstrap:: <!-- Bootstrap --> <script type="text/javascript" src="https://netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> <link rel="stylesheet" h

Bootstrap dropdown doesn't work after first submit ajax form

Brian Powell I have a dropdown menu on my page manager.php, here. sorry, formatting-bootstrap:: <!-- Bootstrap --> <script type="text/javascript" src="https://netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> <link rel="stylesheet" h

Bootstrap dropdown doesn't work after first submit ajax form

Brian Powell I have a dropdown menu on my page manager.php, here. sorry, formatting-bootstrap:: <!-- Bootstrap --> <script type="text/javascript" src="https://netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> <link rel="stylesheet" h

Bootstrap dropdown doesn't work after first submit ajax form

Brian Powell I have a dropdown menu on my page manager.php, here. sorry, formatting-bootstrap:: <!-- Bootstrap --> <script type="text/javascript" src="https://netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> <link rel="stylesheet" h

jQuery click doesn't work but jQuery is loaded

DazDylz I don't understand why this jQuery script doesn't work; $( "#send" ).click(function() { console.log("ee"); }); console.log("test"); When the page loads, I see "Test" in the console, so the jQuery page is loaded. But when I click #send it won't wo

jQuery click doesn't work but jQuery is loaded

DazDylz I don't understand why this jQuery script doesn't work; $( "#send" ).click(function() { console.log("ee"); }); console.log("test"); When the page loads, I see "Test" in the console, so the jQuery page is loaded. But when I click #send it won't wo

Form in Ajax loaded content doesn't work

who dr I have a page that loads content from other files (content.php)into a div that reloads every 5 seconds. In the content.php file I am using a form (simple HTML without javascript). When I access the file directly, (example.com/content.php)the form works,

Length doesn't work for ajax loaded content

Rayhan Porohit Here is my HTML <li><a class="tab-click" href="#tab-4" data-tab-current="about-us">About Us</a></li> Here is my jQuery code $(document).on('click', '.tab-click a', function(event) { if($('.s-tab').length) { event.prev

Navigation tab doesn't change with click in bootstrap

Man · ((Peyman Omidi) I've created two tabs for "Fixtures" and "Results", but when I click on the "Results" tab, nothing changes and it remains in the "Fixtures" tab! Here is my code: <section> <div class="container"> <div class="row"> <div

Navigation tab doesn't change with click in bootstrap

Man · ((Peyman Omidi) I've created two tabs for "Fixtures" and "Results", but when I click on the "Results" tab, nothing changes and it remains in the "Fixtures" tab! Here is my code: <section> <div class="container"> <div class="row"> <div