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 #testCheckBoxid 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 DriverLicense   

$('.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
        
     });
<linkhref="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"rel="stylesheet"/><style>.panel-body {
    padding: 4px50px;
}
</style><divclass="panel-group driving-license-settings"id="accordion"><divclass="panel panel-default"><divclass="panel-heading"><h4class="panel-title"><divclass="checkbox"><labeldata-toggle="collapse"data-target="#collapseOne"aria-expanded="false"aria-controls="collapseOne"><inputtype="checkbox"id="check"/> I have Driver License  
                    </label></div></h4></div><divid="collapseOne"class="panel-collapse collapse in"><divclass="panel-body"><divclass="driving-license-kind"><divclass="checkbox"><inputtype="checkbox"value="">A</div><divclass="checkbox"><inputtype="checkbox"value="">B</div><divclass="checkbox"><inputtype="checkbox"value="">C</div><divclass="checkbox"><inputtype="checkbox"value="">D</div><divclass="checkbox"><inputtype="checkbox"value="">E</div></div></div></div></div></div><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><scriptsrc="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?"