I am making a Rails app, and am trying to achieve a particular functionality relating to Twitter\'s Bootstrap collapse. Bear with me as I explain it.
I currently hav
The Method Works Properly For me:
var lanopt = $(".language-option");
lanopt.on("show.bs.collapse",".collapse", function(){
lanopt.find(".collapse.in").collapse("hide");
});
If you are using Bootstrap 4, and you don't want to change your markup:
var $myGroup = $('#myGroup');
$myGroup.on('show.bs.collapse','.collapse', function() {
$myGroup.find('.collapse.show').collapse('hide');
});
In bootstrap 4 to close all collapseds works like this:
ACTION:
<button id="CloseAll" class="btn btn-primary" type="button" data-toggle="collapse">Close All</button>
JQUERY:
$(document).ready(function() {
$("#CloseAll").on('click', function() {
$(".collapse").removeClass("show");
});
});
This was helpful for me:
jQuery('button').click( function(e) {
jQuery('.in').collapse('hide');
});
It's collapsed already open section. Thnks to GrafiCode Studio
Using data-parent
, first solution is to stick to the example selector architecture
<div id="myGroup">
<button class="btn dropdown" data-toggle="collapse" data-target="#keys" data-parent="#myGroup"><i class="icon-chevron-right"></i> Keys <span class="badge badge-info pull-right">X</span></button>
<button class="btn dropdown" data-toggle="collapse" data-target="#attrs" data-parent="#myGroup"><i class="icon-chevron-right"></i> Attributes</button>
<button class="btn dropdown" data-toggle="collapse" data-target="#edit" data-parent="#myGroup"><i class="icon-chevron-right"></i> Edit Details</button>
<div class="accordion-group">
<div class="collapse indent" id="keys">
keys
</div>
<div class="collapse indent" id="attrs">
attrs
</div>
<div class="collapse" id="edit">
edit
</div>
</div>
</div>
Demo (jsfiddle)
Second solution is to bind on the events and hide the other collapsible elements yourself.
var $myGroup = $('#myGroup');
$myGroup.on('show.bs.collapse','.collapse', function() {
$myGroup.find('.collapse.in').collapse('hide');
});
Demo (jsfiddle)
PS: the strange effect in the demos is caused by the min-height
set for the example, just ignore that.
Edit: changed the JS event from show
to show.bs.collapse
as specified in Bootstrap documentation.
working like a charm here for bootstrap 4>4.1.1
var myGroup = $('your-list');
myGroup.on('show.bs.collapse','.collapse', function() {
myGroup.find('.collapse.show').collapse('hide');
});