Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Calling Bootstrap Dropdown Methods via JavaScript</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script> <script> document.addEventListener("DOMContentLoaded", function(){ var showBtn = document.getElementById("showBtn"); var hideBtn = document.getElementById("hideBtn"); var toggleBtn = document.getElementById("toggleBtn"); var updateBtn = document.getElementById("updateBtn"); var disposeBtn = document.getElementById("disposeBtn"); // Create a dropdown instance var myDropdown = new bootstrap.Dropdown(document.getElementById("myDropdown")); showBtn.addEventListener("click", function(){ myDropdown.show(); }); hideBtn.addEventListener("click", function(){ myDropdown.hide(); }); toggleBtn.addEventListener("click", function(){ myDropdown.toggle(); }); updateBtn.addEventListener("click", function(){ myDropdown.update(); }); disposeBtn.addEventListener("click", function(){ myDropdown.dispose(); }); }); </script> </head> <body> <div class="m-5"> <button type="button" class="btn btn-secondary" id="showBtn">Show</button> <button type="button" class="btn btn-secondary" id="hideBtn">Hide</button> <button type="button" class="btn btn-secondary" id="toggleBtn">Toggle</button> <button type="button" class="btn btn-secondary" id="updateBtn">Update</button> <button type="button" class="btn btn-danger" id="disposeBtn">Dispose</button> <hr> <div class="dropdown"> <div class="btn-group"> <button type="button" class="btn btn-primary dropdown-toggle" id="myDropdown">Dropdown</button> <div class="dropdown-menu"> <a href="#" class="dropdown-item">Action</a> <a href="#" class="dropdown-item">Another action</a> <div class="dropdown-divider"></div> <a href="#" class="dropdown-item">Separated link</a> </div> </div> </div> </div> </body> </html>