Bootstrap collapse on radio button
Bootstrap and css only solution:
Remove the panel's id and add a unique class to the class instead. Change the data-target
selector for .collapseOne:not(.in)
on "Yes" and .collapseOne:not.in
for "No".
<h2>Bootstrap collapse panel with radio buttons</h2>
<br />
<div class="container">
<div class="row">
<div class="col-xs-12">
<span>Display panel: </span>
<input name="collapseGroup" type="radio" data-toggle="collapse" data-target=".collapseOne:not(.in)"/> Yes
<input name="collapseGroup" type="radio" data-toggle="collapse" data-target=".collapseOne.in" checked/> No
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
Header
</h4>
</div>
<div class="collapseOne panel-collapse collapse">
<div class="panel-body">
<p>Content</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
You can remove data-toggle="collapse"
and data-target="#collapseOne"
Add values to your inputs value="yes"
and value="no"
And hide/show collapse with javascript on input change event
$('[name="collapseGroup"]').on('change', function() {
if($(this).val() === "yes") {
$('#collapseOne').collapse('show');
} else {
$('#collapseOne').collapse('hide');
}
});
Codepen