skip to Main Content

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

4

Answers


  1. 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>
    Login or Signup to reply.
  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);
    });
    
    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
  4. 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);
            });
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search