1
I have a problem in script below, the function myFunction
and myFunction1
normally open the dropdown by clicking the button, but in the click-out and close part, 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>
Why do you overwrite the
window.onclick
when you enter the second function. Why not treat everything in the same event?– Woss
I tried to put everything into the same event but it didn’t work the same way.
– Rafael Meirim
You can show how you did it?
– Woss
I took out the second event, took his IF and put it together with the first.
– Rafael Meirim
So it should work.
– Woss
Have you tried placing console.log(Event) inside the window.onclick = Function(Event) { to see if something appears on the console? or even inside the if to see if it is entering
– André Vicente
Putz worked, was calling another 'show'. Thanks for the tip
– Rafael Meirim