Complete the code to show the dropdown menu when the button is clicked.
<button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="[1]" aria-expanded="false"> Dropdown button </button>
The attribute data-bs-toggle="dropdown" tells Bootstrap to toggle the dropdown menu when the button is clicked.
Complete the code to listen for the event when the dropdown menu is about to be shown.
var dropdownElement = document.getElementById('dropdownMenuButton'); dropdownElement.addEventListener('[1]', function (event) { console.log('Dropdown is about to be shown'); });
The event show.bs.dropdown fires just before the dropdown menu is shown.
Fix the error in the code to programmatically hide the dropdown menu.
var dropdownElement = document.getElementById('dropdownMenuButton'); var dropdown = bootstrap.Dropdown.getInstance(dropdownElement); dropdown.[1]();
The method hide() closes the dropdown menu programmatically.
Fill both blanks to create a dropdown and listen for when it has fully hidden.
<button class="btn btn-secondary dropdown-toggle" type="button" id="menuBtn" data-bs-toggle="[1]" aria-expanded="false"> Menu </button> <script> var btn = document.getElementById('menuBtn'); btn.addEventListener('[2]', function () { alert('Dropdown has fully hidden'); }); </script>
Use data-bs-toggle="dropdown" to enable dropdown toggling. The event hidden.bs.dropdown fires after the dropdown is fully hidden.
Fill all three blanks to create a dropdown, listen for when it is shown, and programmatically toggle it.
<button class="btn btn-info dropdown-toggle" type="button" id="toggleBtn" data-bs-toggle="[1]" aria-expanded="false"> Toggle Dropdown </button> <script> var toggleBtn = document.getElementById('toggleBtn'); toggleBtn.addEventListener('[2]', function () { console.log('Dropdown is now shown'); }); var dropdownInstance = bootstrap.Dropdown.getOrCreateInstance(toggleBtn); dropdownInstance.[3](); </script>
Use data-bs-toggle="dropdown" to enable dropdown toggling. Listen for shown.bs.dropdown event after the dropdown is shown. Use toggle() method to open or close the dropdown programmatically.
