Skip to content Skip to sidebar Skip to footer

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/

Solution 1:

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


Solution 2:

Disable the checkbox while collapsing:

http://jsfiddle.net/L0h3s7uf/220/

<input id="license_check_box" type="checkbox"/> I have Driver License   

$('.collapse').on('show.bs.collapse hide.bs.collapse', function() {
    $('#license_check_box').prop('disabled', true);
}).on('shown.bs.collapse hidden.bs.collapse', function() {
    $('#license_check_box').prop('disabled', false);
});

Solution 3:

This is a general solution copied from @IanMetten to get all and fix collapsable controls.

$('[data-toggle="collapse"]').each(function() {
        var $control=$(this);       
        var $target=$($control.attr("data-target"));
        $target.on('show.bs.collapse hide.bs.collapse', function() {
            $control.prop('disabled', true);
        }).on('shown.bs.collapse hidden.bs.collapse', function() {
            $control.prop('disabled', false);
        });
    });

Solution 4:

I think this will work for You

$('.collapse').collapse()
$("#check").one('click', function (event) {  
           event.preventDefault();
           //do something
        
     });
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<style>
.panel-body {
    padding: 4px 50px;
}
</style>
     
    <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" aria-expanded="false" aria-controls="collapseOne">
                        <input type="checkbox" id="check"/> 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>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

Post a Comment for "Why Bootstrap 3 Collapse Is Not Synchronized With Checkbox State On Double Click?"