Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery show.bs.collapse event for nested Bootstrap collapsible buttons fires on more buttons than expected

I'm using Bootstrap to make collapsible nested divs/buttons. I want to collapse all siblings of a div when it is expanded and to collapse all children of a div when it is collapsed (so that the expanded divs are from only one path from one of the root divs to an inner div).

I choose to do it with jQuery and not using Bootstrap's Accordion - just buttons with associated divs.

So I tried to select all divs that could expand and use the show.bs.collapse event, so that I would know which div was expanded.

$(function () {
  $(".collapse").on('show.bs.collapse', function () {
    console.log ($(this).context.id);
  })
});

If my tree is like so:

  • 1 - expanded

    • 2 - expanded

      • 3 - collapsed
    • 4 - collapsed

  • 5 - collapsed

  • 6 - collapsed

and I expand 3, I don't get:

3

but:

3
2
1

Why is show.bs.collapse applied to divs 1 and 2?

like image 972
Nested Buttons Avatar asked Aug 18 '15 10:08

Nested Buttons


1 Answers

Try

$(function() {
  $(".collapse").on('show.bs.collapse', function(e) {
    if ($(this).is(e.target)) {
      snippet.log(this.id)
    }
  })
});
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

<script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap-theme.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.js"></script>

<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingOne">
      <h4 class="panel-title">
        <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          Collapsible Group Item #1
        </a>
      </h4>
    </div>
    <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
      <div class="panel-body">
        <div class="panel-group" id="accordion1-1" role="tablist" aria-multiselectable="true">
          <div class="panel panel-default">
            <div class="panel-heading" role="tab" id="headingOne-1">
              <h4 class="panel-title">
                <a role="button" data-toggle="collapse" data-parent="#accordion1-1" href="#collapseOne-1" aria-expanded="true" aria-controls="collapseOne">
                  Collapsible Group Item #1-1
                </a>
              </h4>
            </div>
            <div id="collapseOne-1" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne-1">
              <div class="panel-body">
                Collapsible Group Item #1-1
              </div>
            </div>
          </div>
          <div class="panel panel-default">
            <div class="panel-heading" role="tab" id="headingTwo-1">
              <h4 class="panel-title">
                <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion1-1" href="#collapseTwo-1" aria-expanded="false" aria-controls="collapseTwo">
                  Collapsible Group Item #2-1
                </a>
              </h4>
            </div>
            <div id="collapseTwo-1" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo-1">
              <div class="panel-body">
                <div class="panel-group" id="accordion1-1-2" role="tablist" aria-multiselectable="true">
                  <div class="panel panel-default">
                    <div class="panel-heading" role="tab" id="headingOne-1-2">
                      <h4 class="panel-title">
                        <a role="button" data-toggle="collapse" data-parent="#accordion1-1-2" href="#collapseOne-1-2" aria-expanded="true" aria-controls="collapseOne">
                          Collapsible Group Item #1-1-2
                        </a>
                      </h4>
                    </div>
                    <div id="collapseOne-1-2" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne-1-2">
                      <div class="panel-body">
                        Collapsible Group Item #1-1-2
                      </div>
                    </div>
                  </div>
                  <div class="panel panel-default">
                    <div class="panel-heading" role="tab" id="headingTwo-1-2">
                      <h4 class="panel-title">
                        <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion1-1-2" href="#collapseTwo-1-2" aria-expanded="false" aria-controls="collapseTwo">
                          Collapsible Group Item #2-1-2
                        </a>
                      </h4>
                    </div>
                    <div id="collapseTwo-1-2" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo-1-2">
                      <div class="panel-body">
                        Collapsible Group Item #2-1-2
                      </div>
                    </div>
                  </div>
                  <div class="panel panel-default">
                    <div class="panel-heading" role="tab" id="headingThree-1-2">
                      <h4 class="panel-title">
                        <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion1-1-2" href="#collapseThree-1-2" aria-expanded="false" aria-controls="collapseThree-1-2">
                          Collapsible Group Item #3-1-2
                        </a>
                      </h4>
                    </div>
                    <div id="collapseThree-1-2" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree-1-2">
                      <div class="panel-body">
                        Collapsible Group Item #3-1-2
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="panel panel-default">
            <div class="panel-heading" role="tab" id="headingThree-1">
              <h4 class="panel-title">
                <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion1-1" href="#collapseThree-1" aria-expanded="false" aria-controls="collapseThree-1">
                  Collapsible Group Item #3-1
                </a>
              </h4>
            </div>
            <div id="collapseThree-1" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree-1">
              <div class="panel-body">
                Collapsible Group Item #3-1
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingTwo">
      <h4 class="panel-title">
        <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          Collapsible Group Item #2
        </a>
      </h4>
    </div>
    <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
      <div class="panel-body">
        Collapsible Group Item #2
      </div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingThree">
      <h4 class="panel-title">
        <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
          Collapsible Group Item #3
        </a>
      </h4>
    </div>
    <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
      <div class="panel-body">
        Collapsible Group Item #3
      </div>
    </div>
  </div>
</div>
like image 110
Arun P Johny Avatar answered Nov 15 '22 17:11

Arun P Johny