Skip to content
Advertisement

Why Bootstrap 3 Collapse is not synchronized with checkbox state on double click?

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/

<div class="panel-group driving-license-settings" id="accordion">
    <div class="panel panel-default">
        <div class="panel-heading">
             <h4 class="panel-title">
                                      <div class="checkbox">
                <label data-toggle="collapse" data-target="#collapseOne">
                    <input type="checkbox"/> I have Driver License  
                </label>
            </div>
                                  </h4>

        </div>
        <div id="collapseOne" class="panel-collapse collapse in">
            <div class="panel-body">
                <div class="driving-license-kind">
                    <div class="checkbox">
                        <input type="checkbox" value="">A</div>
                    <div class="checkbox">
                        <input type="checkbox" value="">B</div>
                    <div class="checkbox">
                        <input type="checkbox" value="">C</div>
                    <div class="checkbox">
                        <input type="checkbox" value="">D</div>
                    <div class="checkbox">
                        <input type="checkbox" value="">E</div>
                </div>
            </div>
        </div>
    </div>
</div>

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:

$('.collapse').collapse();

$("#testCheckBox :checkbox").bind('click dblclick', function(evt) {
    console.log(evt.type);

    if ($(this).is(":checked")) {
        $('.collapse').slideDown('fast');
    } else {
        $('.collapse').slideUp('fast');
    }
})

demo: https://jsbin.com/ciloliweto/edit?html,output

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement