Estou com um problema no script abaixo, a função myFunction
e myFunction1
abrem normalmente o dropdown ao clicar o botão, mas na parte de clicar fora e fechar, apenas o segundo evento está funcionando.
<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>
window.onclick
quando insere a segunda função. Por quê não trata tudo no mesmo evento?