i am trying to setup a dropdown menu, that can be closed by clicking outside (the opened div) and by clicking the button/img (that opens the div).
Image with onclick funtion:
<img onclick="hide()" id="menu" src="....">
Dropdown list that should be opened and closed, class declares dropdown as display:none:
<ul id="dropdown" class="dropdown-breadcrumb">
<li>...</li>
</ul>
This is my solution so far:
function hide() {
var x = document.getElementById("dropdown");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
When i am trying to add a function that closes the dropdown when the user clicks outside the dropdown:
window.addEventListener('mouseup',function(event){
var dropd = document.getElementById('dropdown');
if(event.target != dropd && event.target.parentNode != dropd){
dropd.style.display = 'none';
}
});
Then i end up with a opened dropdown because the onclick-function starts.
Can someone help me to combine those functions? Thank you!