According to this question: “Twitter Bootstrap 3 collapse when checkbox checked” i’ve tried this solution, because it is simple and clean.
http://jsfiddle.net/L0h3s7uf/1/
JavaScript
x
31
31
1
<div class="panel-group driving-license-settings" id="accordion">
2
<div class="panel panel-default">
3
<div class="panel-heading">
4
<h4 class="panel-title">
5
<div class="checkbox">
6
<label data-toggle="collapse" data-target="#collapseOne">
7
<input type="checkbox"/> I have Driver License
8
</label>
9
</div>
10
</h4>
11
12
</div>
13
<div id="collapseOne" class="panel-collapse collapse in">
14
<div class="panel-body">
15
<div class="driving-license-kind">
16
<div class="checkbox">
17
<input type="checkbox" value="">A</div>
18
<div class="checkbox">
19
<input type="checkbox" value="">B</div>
20
<div class="checkbox">
21
<input type="checkbox" value="">C</div>
22
<div class="checkbox">
23
<input type="checkbox" value="">D</div>
24
<div class="checkbox">
25
<input type="checkbox" value="">E</div>
26
</div>
27
</div>
28
</div>
29
</div>
30
</div>
31
But with this “solution” you have a problem. If you click the checkbox too fast, the area is collapsed but the checkbox is checked.
How can i prevent this double clicking problem?
I am using:
- Bootstrap 3
- jQuery 3.1.1
Advertisement
Answer
Since there is no double click handling in bootstrap for toggle specially, so I come up with a special work around to make double click in sync.
I just removed data-toggle="collapse"
attribute, added #testCheckBox
id to checkbox’s parent div
and I did some custom script which detect if double click or single click then validate the checkbox values and toggle on their bases:
JavaScript
1
12
12
1
$('.collapse').collapse();
2
3
$("#testCheckBox :checkbox").bind('click dblclick', function(evt) {
4
console.log(evt.type);
5
6
if ($(this).is(":checked")) {
7
$('.collapse').slideDown('fast');
8
} else {
9
$('.collapse').slideUp('fast');
10
}
11
})
12