Peter R
Peter R

Reputation: 3506

Bootstrap tabs not working using append

Im using a bootstrap template. It has a popup product modal that it appends certain info to. My bootstrap tabs don't work because they are appended to the modal.

Here is a jsfiddle showing the problem. The first tabs work, but the appended ones dont.

http://jsfiddle.net/wLbLzqcv/

HTML:

<div class="test">
<div role="tabpanel" id="tabs">

  <!-- Nav tabs -->
  <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>

  <!-- Tab panes -->
  <div class="tab-content">
    <div role="tabpanel" class="tab-pane active" id="home">A</div>
    <div role="tabpanel" class="tab-pane" id="profile">B</div>
    <div role="tabpanel" class="tab-pane" id="messages">C</div>
    <div role="tabpanel" class="tab-pane" id="settings">D</div>
  </div>

</div>
</div>

JS:

$('.test').append($('#tabs').html())

Upvotes: 0

Views: 1314

Answers (1)

hapass
hapass

Reputation: 322

Here is a working solution:

http://jsfiddle.net/wLbLzqcv/1/

It's not pretty, but it highlights what is wrong with your code. Your id's shouldn't duplicate.

var html = $('.test').html();

var $html = $(html);

$html.find(".tab-pane").each(function(){
    var $tabPane = $(this);
    $tabPane.attr("id", $tabPane.attr("id") + "newid");
})

$html.find("a[data-toggle='tab']").each(function(){
    var $anchor = $(this);
    $anchor.attr("href", $anchor.attr("href") + "newid");
})

$('.test').append($html)

Upvotes: 1

Related Questions