Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!