1

I'm trying to show a dropdown via JS. Unfortunately it seems like there's a bug in Bootstrap 5. In this example code, showing the modal works, but showing the dropdown throws an exception: Uncaught TypeError: Cannot read property 'classList' of undefined at _e.show (dropdown.js:140)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css">
</head>

<body>

<div id="testDropdown" class="dropdown">
    <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown">
        Dropdown button
    </button>
    <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="#">Action</a></li>
        <li><a class="dropdown-item" href="#">Another action</a></li>
        <li><a class="dropdown-item" href="#">Something else here</a></li>
    </ul>
</div>

<div id="testModal" class="modal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Modal title</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>Modal body text goes here.</p>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js"></script>

<script>

    let testModal = document.getElementById("testModal");
    //new bootstrap.Modal(testModal).show(); //works

    let testDropdown = document.getElementById("testDropdown");
    new bootstrap.Dropdown(testDropdown).show(); //doesnt work

</script>

</body>
</html>

Am I doing something wrong or is this a bug?

Thanks in advance.

nhcodes
  • 1,206
  • 8
  • 20

2 Answers2

2

The element should be the dropdown-trigger instead of the parent dropdown...

<div class="dropdown">
    <button class="btn btn-secondary dropdown-toggle" id="testDropdown" type="button" data-bs-toggle="dropdown"> Dropdown button </button>
    <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="#">Action</a></li>
        <li><a class="dropdown-item" href="#">Another action</a></li>
        <li><a class="dropdown-item" href="#">Something else here</a></li>
    </ul>
</div>

Demo

Note: Bootstrap 5 includes a new auto-close option. This must be set to false in order to programmatically trigger the dropdown from outside the dropdown's parent.

Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
  • Thanks it works! However in Bootstrap 4 it was possible to show it without using a toggle button, any idea if that's possible in Bootstrap 5? – nhcodes Apr 18 '21 at 17:47
  • Please accept the answer since it resolves the question. Do you have an example of this in Bootstrap 4 .. which would use jQuery not vanilla JS? All the [answers here show using the toggle](https://stackoverflow.com/questions/22842903/how-to-open-bootstrap-dropdown-programmatically) – Carol Skelly Apr 18 '21 at 18:26
  • In Bootstrap 4 this worked: html: `` js: `$(document.getElementById("DROPDOWN_SEARCH")).dropdown('show');` – nhcodes Apr 18 '21 at 19:03
  • I found a solution, see my other answer for details. – nhcodes Apr 18 '21 at 20:07
-1

I got it working without using a button:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Dropdown Test</title>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.1/font/bootstrap-icons.css">
</head>

<body>

<div class="container-fluid">

    <div class="row justify-content-center">
        <div class="w-75 mt-2">

            <div class="input-group">
                <span class="input-group-text"><i class="bi bi-search"></i></span>
                <input type="text" class="form-control" placeholder="Search for something">
                <button class="btn btn-primary" type="button">Search</button>
            </div>

            <div class="dropdown">
                <div data-bs-toggle="dropdown" id="DROPDOWN_SEARCH"></div>
                <div class="dropdown-menu w-100">
                    <a class="dropdown-item" href="#">Result 1</a>
                    <a class="dropdown-item" href="#">Result 2</a>
                    <a class="dropdown-item" href="#">Result 3</a>
                </div>
            </div>

        </div>
    </div>

</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>

<script>
    let searchDropdown = new bootstrap.Dropdown("#DROPDOWN_SEARCH");
    searchDropdown.show();
</script>

</body>

</html>
nhcodes
  • 1,206
  • 8
  • 20
  • This doesn't seem to actually work. The dropdown is not toggle-able. – Carol Skelly Jul 29 '21 at 12:52
  • Running the code snippet above works for me.. You might have this problem: https://stackoverflow.com/questions/10480697/keep-bootstrap-dropdown-open-on-click dropdown.show() won't work in an onclick event because the dropdown gets immediately closed. – nhcodes Oct 16 '21 at 12:37