Phillip Senn
Phillip Senn

Reputation: 47595

event listener hide.bs.dropdown not firing in bootstrap 5

function bsDropdown(e) {
  e.addEventListener('show.bs.dropdown',yay)
  e.addEventListener('hide.bs.dropdown',nay)
}
function yay() {
document.getElementById('yay').innerHTML = 'yay'
}
function nay() {
document.getElementById('nay').innerHTML = 'nay'
}
document.querySelectorAll('.dropdown-toggle').forEach(bsDropdown)
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<p id="yay">1</p>
<p id="nay">2</p>

<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
    Dropdown button
  </button>
  <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <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>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>

Upvotes: 1

Views: 1955

Answers (1)

Hemant
Hemant

Reputation: 1156

You might be looking for this Dropdown Events Issue

The closest behaviour can still be achieved like this

document.body.addEventListener("click", nay);
function bsDropdown(e) {
    e.addEventListener("show.bs.dropdown", yay);
}
function yay() {
    document.getElementById("yay").innerHTML = "yay";
}
function nay() {
    if (document.getElementById("dropdownMenuButton").classList.contains("show")) {
        document.getElementById("nay").innerHTML = "nay";
    }
}
document.querySelectorAll(".dropdown-toggle").forEach(bsDropdown);
body{
height:100vh;}
<link
            href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
            rel="stylesheet"
            integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1"
            crossorigin="anonymous"
        />

        <p id="yay">1</p>
        <p id="nay">2</p>

        <div class="dropdown">
            <button
                class="btn btn-secondary dropdown-toggle"
                type="button"
                id="dropdownMenuButton"
                data-bs-toggle="dropdown"
                aria-expanded="false"
            >
                Dropdown button
            </button>
            <ul class="dropdown-menu hide" aria-labelledby="dropdownMenuButton">
                <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>
        <script
            src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
            integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW"
            crossorigin="anonymous"
        ></script>
I have kept body height to 100vh to cover entire screen even when content is very small, i know that body eventListener will be triggered on every click on body, but i don't it will cause any issue.

Upvotes: 2

Related Questions