Bootstrap tabs are not working via AJAX in a modal
Asked Answered
L

3

5

It working fine without modal and ajax. But when I load tabbed panel in modal using ajax it does not select Tab Panes.

<!-- Nav tabs -->
<ul class="nav nav-tabs tabs-left">
    <li class="active tab-selection"><a data-toggle="tab" href="#financial">Financial</a></li>
    <li class="tab-selection"><a data-toggle="tab" href="#marketing">Marketing</a></li>
</ul>

<!-- Tab panes -->
<div class="tab-content">
  <div class="tab-pane active" id="financial">Financial Tab</div>
  <div class="tab-pane" id="marketing">Marketing Tab</div>      
</div>

AJAX

$.ajax({
        url : url,
        type : "POST", 
        success:function(data){
            $(".modal-body").html(data);
        }
  });

My Modal

<div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="previewForm" class="modal fade">
  <div class="modal-dialog" style="width: 675px">
    <div class="modal-content">
    <div class="modal-header">
        <button data-dismiss="modal" class="close" type="button"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
        <h3 id="myModalLabel" class="modal-title">Preview</h3>
    </div>
        <div class="modal-body">
        </div>  
    </div>
 </div>
</div>
Liqueur answered 30/1, 2015 at 11:45 Comment(5)
You're trying to add some HTML to an element with class .modal-body, but there is no element with that class in your HTML code.Punctuality
Code structure is fine and modal is also there in my code. Without modal it working fine also.Liqueur
I can't see modal anywhere in the code you've posted, could you post some more?Punctuality
I can suggest loading tab content by tab clickCyme
@Punctuality I updated question with my modal codeLiqueur
L
7

I found alternative solution to show tabbed-panel of associated nav-tab like following.

$(document).ready(function(){
    $(document).on("click",".modal-body li a",function()
    {
        tab = $(this).attr("href");
        $(".modal-body .tab-content div").each(function(){
            $(this).removeClass("active");
        });
        $(".modal-body .tab-content "+tab).addClass("active");
    });
});
Liqueur answered 30/1, 2015 at 13:48 Comment(0)
A
2

Tab with data-attribute is execute on document load. You're calling it by ajax, so just init the tab component on success.

Here is an exemple (not with ajax, but with button click) :

Bootply : http://www.bootply.com/sNQH8hGFY9

Your code :

 $.ajax({
            url : url,
            type : "POST", 
            success:function(data){
                $(".modal-body").html(data);
                $('.modal-body .nav').tab();  //  <--- Look Here
            }
      });
Angeli answered 30/1, 2015 at 12:18 Comment(1)
Thanks for your co-operate but Nav-tab is work but it associated tabbed-panel not workingLiqueur
M
1

Also make sure the "tab's" id are unique to the page.

Minoru answered 16/8, 2015 at 0:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.