Building Dynamic User Interfaces with JavaScript
xxxxxxxxxx
// Get the dropdown element
const dropdown = document.getElementById("myDropdown");
// Add a click event listener to the button that opens the dropdown
document.getElementById("myButton").addEventListener("click", function() {
dropdown.classList.toggle("show");
});
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
dropdown.classList.remove("show");
}
}