user8604852
user8604852

Reputation: 1

Why "show/shown/hide/hidden.bs.collapse" doesn't get triggered on my nested collapse element?

I have a button. Clicking on the button collapses a div. In that collapsed div there is another button. When clicking that button it collapses another div.

Now the collapse and all that works fine, the problem I have lies on the events.

I tried:

  // this fires when I click parent button, 
  // but it also fires when I click the child button << problem
  $("#parentDiv").on("show.bs.collapse", () => {
      console.log("parent");
    });

  // this never fires << problem
  $("#childDiv").on("show.bs.collapse", () => {
      console.log("child");
    });

Html:

<button data-toggle="collapse" data-target="#parentDiv" aria-controls="parentDiv"
        aria-expanded="false" aria-label="Toggle parent">Show parent
</button>

<div class="collapse" id="parentDiv">
  <p>This is the parent</p>

  <button data-toggle="collapse" data-target="#childDiv" aria-controls="childDiv" 
          aria-expanded="false" aria-label="Toggle child">Show child
  </button>

  <div class="collapse" id="childDiv">
    <p>This is the child</p>
  </div>
</div>

What I ended up using and why.

I first tried using the accepted answer, as that is how it is supposed to be done, to bad that jQuery doesn't work well when I change routes, it breaks.

I ended up using some kind of code that looks like the accepted answer of the possible duplicate, as that seemed the only way to get jQuery to work reliably.

$(document).ready(() => {
   $("#parentDiv").on("show.bs.collapse", (e) => {
     if (e.target.id == "childDiv") {
       //enter code here
     }
   })
}); 

Upvotes: 0

Views: 3942

Answers (1)

Karthikeyan
Karthikeyan

Reputation: 302

In order to prevent parent event listener execution on on click of child, just add event.stopPropagation to prevent the event bubbling up.

// this fires when I click parent button, 
  
  $("#parentDiv").on("show.bs.collapse", () => {
      console.log("parent");
    });

  // this fires on click of child button
  $("#childDiv").on("show.bs.collapse", (e) => {
        //prevent event bubbling up to parent listener
  		e.stopPropagation();
      console.log("child");
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.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>


<button data-toggle="collapse" data-target="#parentDiv" aria-controls="parentDiv"
        aria-expanded="false" aria-label="Toggle parent">Show parent
</button>

<div class="collapse" id="parentDiv">
  <p>This is the parent</p>

  <button data-toggle="collapse" data-target="#childDiv" aria-controls="childDiv" 
          aria-expanded="false" aria-label="Toggle child">Show child
  </button>

  <div class="collapse" id="childDiv">
    <p>This is the child</p>
  </div>
</div>

Upvotes: 2

Related Questions