Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
Drop down menu doesn't comedown after display set to none

I have a drop down menu. I added a javascript addevent listener for the dropdown content to close when i click on an option. It works fine.

Javascript

const element = document.querySelector(".dropdown-content");


element.addEventListener("click", () => {
    
    element.style.display ="none";
    
}); 


  <div class="dropdown" id ="dropdown" >
  <button class="dropbtn" id ="dropbtn" >Select Trailer  <i class="icon ion-location"></i></button>
  
  <div class="dropdown-content" id ="dropdown-content">
    <a class = "drop" id ="drop" href="#">Option1<br></a> 
    <a class = "drop" id ="drop" href="#">Option2<br></a> 
  </div>
  
</div>

css

.dropbtn {
  background-color: #F6A695;
  color: white;
  padding: 16px;
  font-size: 18px;
  border: none;
}

.dropdown {
  position: relative;
  display: inline-block;
  padding-bottom:80px;
 
}

.dropdown-content {
   display: none; 
  position: absolute; 
  background-color: #f1f1f1;
  min-width: 142px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
   max-height:120px;
   overflow-y:scroll; 
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
   display: block; 
}

.dropdown-content a:hover {background-color: #ddd;}

.dropdown:hover .dropdown-content {display: block;}

.dropdown:hover .dropbtn {background-color: #F4929D;}

ISSUE:

The problem im facing is that when the dropdown content closes on click it wont open again , and it remains closed.

What i have tried :

I have tried changing the display to block when clicking on the drop down again, but it doesn't seem to work.

const element = document.querySelector(".dropdown-content");
const mainbutton = document.querySelector(".dropdown");

mainbutton.addEventListener("click", () => {
    
    element.style.display ="block";
    
});

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use the 'mouseenter' and 'mouseleave' event.

const element = document.querySelector(".dropdown-content");
const mainbutton = document.querySelector(".dropdown");

mainbutton.addEventListener("mouseenter", () => {
   
    element.style.display ="block";
    
});

mainbutton.addEventListener("mouseleave", () => {
    
    element.style.display ="none";
    
});

element.addEventListener("click", () => {
    
    element.style.display ="none";
    
}); 
.dropbtn {
  background-color: #F6A695;
  color: white;
  padding: 16px;
  font-size: 18px;
  border: none;
}

.dropdown {
  position: relative;
  display: inline-block;
  padding-bottom:80px;
 
}

.dropdown-content {
   display: none; 
  position: absolute; 
  background-color: #f1f1f1;
  min-width: 142px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
   max-height:120px;
   overflow-y:scroll; 
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
   display: block; 
}

.dropdown-content a:hover {background-color: #ddd;}

.dropdown:hover .dropdown-content {display: block;}

.dropdown:hover .dropbtn {background-color: #F4929D;}
<div class="dropdown" id ="dropdown" >
  <button class="dropbtn" id ="dropbtn" >Select Trailer  <i class="icon ion-location"></i></button>
  
  <div class="dropdown-content" id ="dropdown-content">
    <a class = "drop" id ="drop" href="#">Option1<br></a> 
    <a class = "drop" id ="drop" href="#">Option2<br></a> 
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

The answer Kirill posted was right but to add on, it worked perfectly when i added this code


const element = document.querySelector(".dropdown-content");
const element2 = document.querySelector(".dropbtn");

element2.addEventListener("mouseenter", () => {
   
    element.style.display ="block";
    
});


element.addEventListener("click", () => {
    
    element.style.display ="none";
    
}); 


about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda