I have simple dropdown button with image inside it:
By clicking on dropdown button, dropItems() is called, which displays list items:
function dropItems() {
document.getElementById("myDropdown").classList.toggle("show");
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn') || !event.target.matches('#dropdownicon')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
<div class="dropdown">
<button onclick="dropItems()" class="dropbtn"><img id="dropdownicon" src="img/dropdown.png" height="25" onclick="dropItems()"></button>
<div id="myDropdown" class="dropdown-content">
<a href="#">Item 1</a>
<a href="#">Item 2</a>
<a href="#">Item 3</a>
</div>
</div>
As you can see, I have also applied onclick event on image inside button, because it blocks button's onclick when you click directly on image.
The problem is that when I click on image, nothing happens.