Search code examples
bootstrap-5bootstrap-accordion

Bootstrap Accordion Throws TypeError: Illegal invocation


I have an accordion written like this

<!DOCTYPE html>
<html>
    <head>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
    </head>
    <body>
    <div id="editTournamentAccordionWrapper" class="accordion accordion-flush">
        <div class="accordion-item">
            <h2 class="accordion-header" id="editTournamentAccordionHeading">
                <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#editTournamentAccordion" aria-expanded="false" aria-controls="editTournamentAccordion">
                    Card title
                </button>
            </h2>
            <div id="editTournamentAccordion" class="accordion-collapse collapse" aria-labelledby="editTournamentAccordionHeading" data-bs-parent="editTournamentAccordionWrapper">
                <div class="accordion-body">
                    <iframe class="w-100" style="height: 1024px;" src="www.example.com"></iframe>
                </div>
            </div>
        </div>
    </div>
    </body>
</html>

When i click the first time on the Accordion header, the accordion won't open and the following exception will be threw

Uncaught TypeError: 'querySelectorAll' called on an object that does not implement interface Element. selector-engine.js:18:59

Any idea of the reason? As far as I noticed, this is the minimum code I had to wrote in order to make the bug present


Solution

  • TLDR: A # is missing in the data-bs-parent attribute.

    According to the Bootstrap documentation, the data-bs-parent accepts a

    selector | jQuery object | DOM element

    When you pass in data-bs-parent="id" for the attribute, it is plain text hence belongs to none of the categories. As a result you get the Uncaught TypeError: Illegal invocation

    Working example:

    <!DOCTYPE html>
    <html>
        <head>
            <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
    
        </head>
        <body>
        <div id="editTournamentAccordionWrapper" class="accordion accordion-flush">
            <div class="accordion-item">
                <h2 class="accordion-header" id="editTournamentAccordionHeading">
                    <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#editTournamentAccordion" aria-expanded="false" aria-controls="editTournamentAccordion">
                        Card title
                    </button>
                </h2>
                <div id="editTournamentAccordion" class="accordion-collapse collapse" aria-labelledby="editTournamentAccordionHeading" data-bs-parent="#editTournamentAccordionWrapper">
                    <div class="accordion-body">
                      Card Body
    <!--                    <iframe class="w-100" style="height: 1024px;" src="www.example.com"></iframe>-->
                    </div>
                </div>
            </div>
        </div>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
        </body>
    </html>