'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/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/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/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  </body>
</html>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source