Skip to content
Advertisement

Why are the event handlers fired twice?

If I collapse or expand “id-1” then the handlers work correctly.

If I collapse or expand “id-1-1” then two handlers are triggered: one for “id-1” and the second for “id-1-1”.

Why is the event fired for “id-1” even though no action is taking place?

const myCollapsible = document.querySelectorAll('.collapse');
myCollapsible.forEach(myOne => {
    myOne.addEventListener('shown.bs.collapse', function() {
        console.log('shown ' + this.id);
    });
    myOne.addEventListener('hidden.bs.collapse', function() {
        console.log('hidden ' + this.id);
    });
});
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.2/font/bootstrap-icons.css">
  </head>
  <body>
    <div>
        <a class="bi-caret-right-fill link-dark text-decoration-none me-2" data-bs-toggle="collapse" href="#id-1"></a>
        <a href="#">id-1</a>
        <div class="collapse ms-4" id="id-1">
            <div>
                <a class="bi-caret-right-fill link-dark text-decoration-none me-2" data-bs-toggle="collapse" href="#id-1-1"></a>
                <a href="#">id-1-1</a>
                <div class="collapse ms-4" id="id-1-1">
                    text
                </div>
            </div>
            <div>
                <a class="bi-caret-right-fill link-dark text-decoration-none me-2" data-bs-toggle="collapse" href="#id-1-2"></a>
                <a href="#">id-1-2</a>
                <div class="collapse ms-4" id="id-1-2">
                    text
                </div>
            </div>
            <div>
                <a class="bi-caret-right-fill link-dark text-decoration-none me-2" data-bs-toggle="collapse" href="#id-1-3"></a>
                <a href="#">id-1-3</a>
                <div class="collapse ms-4" id="id-1-3">
                    text
                </div>
            </div>
        </div>
    </div>
    <div>
        <a class="bi-caret-right-fill link-dark text-decoration-none me-2" data-bs-toggle="collapse" href="#id-2"></a>
        <a href="#">id-2</a>
        <div class="collapse ms-4" id="id-2">
            text
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
  </body>
</html>

Advertisement

Answer

You need to e.stopPropagation() from your child to top level parent.

You can find more on stopPropagation

const myCollapsible = document.querySelectorAll('.collapse');
myCollapsible.forEach(myOne => {
  myOne.addEventListener('shown.bs.collapse', function(e) {
    e.stopPropagation();
    console.log('shown ' + this.id);

  });
  myOne.addEventListener('hidden.bs.collapse', function(e) {
    e.stopPropagation();
    console.log('hidden ' + this.id);

  });
});
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.2/font/bootstrap-icons.css">
</head>

<body>
  <div>
    <a class="bi-caret-right-fill link-dark text-decoration-none me-2" data-bs-toggle="collapse" href="#id-1"></a>
    <a href="#">id-1</a>
    <div class="collapse ms-4" id="id-1">
      <div>
        <a class="bi-caret-right-fill link-dark text-decoration-none me-2" data-bs-toggle="collapse" href="#id-1-1"></a>
        <a href="#">id-1-1</a>
        <div class="collapse ms-4" id="id-1-1">
          text
        </div>
      </div>
      <div>
        <a class="bi-caret-right-fill link-dark text-decoration-none me-2" data-bs-toggle="collapse" href="#id-1-2"></a>
        <a href="#">id-1-2</a>
        <div class="collapse ms-4" id="id-1-2">
          text
        </div>
      </div>
      <div>
        <a class="bi-caret-right-fill link-dark text-decoration-none me-2" data-bs-toggle="collapse" href="#id-1-3"></a>
        <a href="#">id-1-3</a>
        <div class="collapse ms-4" id="id-1-3">
          text
        </div>
      </div>
    </div>
  </div>
  <div>
    <a class="bi-caret-right-fill link-dark text-decoration-none me-2" data-bs-toggle="collapse" href="#id-2"></a>
    <a href="#">id-2</a>
    <div class="collapse ms-4" id="id-2">
      text
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement