It seems that you are looking to toggle the accordion only when the active tab is clicked. You can achieve this by using JavaScript to handle the collapsing functionality. Here is a working example on JSFiddle Here
$('#accordion .panel-heading ul#tabs li a').click(function(){
$("#accordion .panel-heading ul#tabsb li").removeClass('active');
if ($(this).parent('li').hasClass('active'))
{ $('#collapseOne').collapse('toggle'); }
else
{
$('#collapseOne').collapse({toggle:false});
$('#collapseOne').collapse('show');
}
});
$("#accordion .panel-heading ul#tabsb li a").click(function(){
$("#accordion .panel-heading ul#tabs li").removeClass('active');
if ($(this).parent('li').hasClass('active'))
{ $('#collapseTwo').collapse('toggle'); }
else
{
$('#collapseTwo').collapse('show');
}
});
To make the necessary changes in the HTML:
- You have two ul elements with the same id 'tabs'. Change the id of the second ul to 'tabsb'.
- Change the part
href="#collapseOne"
to href=""
as the collapse will be triggered using JavaScript.
- Change the part
href="#collapseTwo"
to href=""
.
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<div data-toggle="collapse" data-parent="#accordion" href="">
<ul id="tabs" class="nav nav-pills" data-tabs="tabs">
<li><a href="#red" data-toggle="tab">Red</a></li>
<li class="active"><a href="#orange" data-toggle="tab">Orange</a></li>
<li><a href="#yellow" data-toggle="tab">Yellow</a></li>
<li><a href="#green" data-toggle="tab">Green</a></li>
<li><a href="#blue" data-toggle="tab">Blue</a></li>
</ul>
</div>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">
<div id="my-tab-content" class="tab-content">
<div class="tab-pane" id="red">
<h1>Red</h1>
<p>red red red red red red</p>
</div>
<div class="tab-pane active" id="orange">
<h1>Orange</h1>
<p>orange orange orange orange orange</p>
</div>
<div class="tab-pane" id="yellow">
<h1>Yellow</h1>
<p>yellow yellow yellow yellow yellow</p>
</div>
<div class="tab-pane" id="green">
<h1>Green</h1>
<p>green green green green green</p>
</div>
<div class="tab-pane" id="blue">
<h1>Blue</h1>
<p>blue blue blue blue blue</p>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<div data-toggle="collapse" data-parent="#accordion" href="">
<ul id="tabsb" class="nav nav-pills" data-tabs="tabsb">
<li><a href="#One" data-toggle="tab">One</a></li>
<li><a href="#To" data-toggle="tab">To</a></li>
<li><a href="#Three" data-toggle="tab">Three</a></li>
<li><a href="#Four" data-toggle="tab">Four</a></li>
</ul>
</div>
</div>
<div id="collapseTwo" class="panel-collapse collapse">
<div class="panel-body">
<div id="my-tab-content" class="tab-content">
<div class="tab-pane" id="One">
<h1>One</h1>
<p>One One One One One One</p>
</div>
<div class="tab-pane active" id="To">
<h1>To</h1>
<p>To To To To To</p>
</div>
<div class="tab-pane" id="Three">
<h1>Three</h1>
<p>Three Three Three Three Three</p>
</div>
<div class="tab-pane" id="green">
<h1>Green</h1>
<p>green green green green green</p>
</div>
<div class="tab-pane" id="Four">
<h1>Four</h1>
<p>Four Four Four Four Four</p>
</div>
</div>
</div>
</div>
</div>
</div>
Update:
Following Jesse Buitenhuis's suggestion, it would be more efficient to use a single class. Add data-collapse-toggle="collapseOne" to the ul element and utilize the following JavaScript code which addresses both of your concerns. View the updated solution on JSFiddle Here
$('#accordion .panel-heading ul li').click(function(){
var target = $('#' + $(this).parent().data('collapse-toggle'));
$("#accordion .panel-heading ul li").not($(this)).removeClass('active');
if ($(this).hasClass('active'))
{ target.collapse('toggle'); }
else
{
target.collapse({toggle:false});
target.collapse('show');
}
});
HTML
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<div data-toggle="collapse" data-parent="#accordion" href="">
<ul id="tabs" class="nav nav-pills" data-collapse-toggle="collapseOne" data-tabs="tabs">
<li><a href="#red" data-toggle="tab">Red</a></li>
<li class="active"><a href="#orange" data-toggle="tab">Orange</a></li>
<li><a href="#yellow" data-toggle="tab">Yellow</a></li>
<li><a href="#green" data-toggle="tab">Green</a></li>
<li><a href="#blue" data-toggle="tab">Blue</a></li>
</ul>
</div>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">
<div id="my-tab-content" class="tab-content">
<div class="tab-pane" id="red">
<h1>Red</h1>
<p>red red red red red red</p>
</div>
<div class="tab-pane active" id="orange">
<h1>Orange</h1>
<p>orange orange orange orange orange</p>
</div>
<div class="tab-pane" id="yellow">
<h1>Yellow</h1>
<p>yellow yellow yellow yellow yellow</p>
</div>
<div class="tab-pane" id="green">
<h1>Green</h1>
<p>green green green green green</p>
</div>
<div class="tab-pane" id="blue">
<h1>Blue</h1>
<p>blue blue blue blue blue</p>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<div data-toggle="collapse" data-parent="#accordion" href="">
<ul id="tabsb" data-collapse-toggle="collapseTwo" class="nav nav-pills" data-tabs="tabs">
<li><a href="#One" data-toggle="tab">One</a></li>
<li><a href="#To" data-toggle="tab">To</a></li>
<li><a href="#Three" data-toggle="tab">Three</a></li>
<li><a href="#Four" data-toggle="tab">Four</a></li>
</ul>
</div>
</div>
<div id="collapseTwo" class="panel-collapse collapse">
<div class="panel-body">
<div id="my-tab-content" class="tab-content">
<div class="tab-pane" id="One">
<h1>One</h1>
<p>One One One One One One</p>
</div>
<div class="tab-pane active" id="To">
<h1>To</h1>
<p>To To To To To</p>
</div>
<div class="tab-pane" id="Three">
<h1>Three</h1>
<p>Three Three Three Three Three</p>
</div>
<div class="tab-pane" id="green">
<h1>Green</h1>
<p>green green green green green</p>
</div>
<div class="tab-pane" id="Four">
<h1>Four</h1>
<p>Four Four Four Four Four</p>
</div>
</div>
</div>
</div>
</div>
</div>