I've been trying to hide/show the bootstrap 4 collapse using checkbox with Jquery
`<script type="text/javascript">
$('.card-header').click(function() {
$(this).find('input[name="question"]').prop('checked', true);
});
</script>`
`<div class="card">
<div class="card-header" id="headingOne">
<div class="checkbox-wrap">
<input class="form-check-input" type="checkbox" name="question" disabled>
</div>
<h2 id="question-1">
<button class="btn btn-link btn-block text-left" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
lorem Ipsome
</button>
</h2>
</div>
<hr>
<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#faq">
<div class="card-body">
<p>lorem posem</p>
</div>
</div>
</div>`
I have used this code. This will show the checkbox checked while it displays show but won't uncheck when it is collapsed
Image
You can check if the button attribute i.e aria-expanded
is true
or false
depending on this checked
or unchecked
checkboxes.
Demo Code :
$('.card-header').click(function() {
//get the attr aria expanded if true means collapse is shown
if ($(this).find('button.btn-link').attr('aria-expanded') === "true") {
$(this).find('input[name="question"]').prop('checked', false);//unchecked
} else {
$(this).find('input[name="question"]').prop('checked', true);
//checked
}
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<div class="card">
<div class="card-header" id="headingOne">
<div class="checkbox-wrap">
<input class="form-check-input" type="checkbox" name="question" disabled>
</div>
<h2 id="question-1">
<button class="btn btn-link btn-block text-left" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
lorem Ipsome
</button>
</h2>
</div>
<hr>
<div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#faq">
<div class="card-body">
<p>lorem posem</p>
</div>
</div>
<div class="card-header" id="headingOne">
<div class="checkbox-wrap">
<input class="form-check-input" type="checkbox" name="question" disabled>
</div>
<h2 id="question-2">
<button class="btn btn-link btn-block text-left" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
lorem Ipsome
</button>
</h2>
</div>
<hr>
<div id="collapseTwo" class="collapse" aria-labelledby="headingOne">
<div class="card-body">
<p>lorem posem</p>
</div>
</div>
</div>
Thank you it worked for me
But still, there is a small bug try to collapse the second div both showing checked
HI, i am not seeing any such behaviour in above demo code .. can you create jsfiddle with original code ? issue might be there
it's ok I have resolved that issue try using radio button