I have a script problem below, the function myFunction
and myFunction1
normally open the dropdown when clicking the button, but in the part of clicking outside and close, only the second event is working.
<script>
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
function myFunction1() {
document.getElementById("myDropdown1").classList.toggle("show");
}
// Fechar ao clicar fora - ESSE NÃO FUNCIONA SE ADICIONAR O DE BAIXO
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
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');
}
}
}
}
// Fechar ao clicar fora - ESSE FUNCIONA, MAS O DE CIMA NÃO
window.onclick = function(event) {
if (!event.target.matches('.btn-header')) {
var dropdowns = document.getElementsByClassName("dropdown-content2");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
</script>