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

101
Views
how to make a javascript event occur only if another event to the same element is first triggered?

I have attached a mouseleave event to the select tag. But I want this event should only occur if a user first clicks the select tag and then removes the mouse from.

function loseFocus() {
    var dateSelect=document.querySelector('[name="dayCount"]');
    dateSelect.blur();
    console.log('mouse leave event triggered')
}
<select name="dayCount" onmouseleave="loseFocus()">
  <option >op1</option>
  <option >op2</option>
  <option>op3</option>
</select>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can define a variable as false and run a click event on your drop down, then in your call back for the click event set that variable to true. Then in your blur event call back a conditional to check if the variable is true.

You may want to do a mouseout event if you don't want to have to click off the drop down menu after a selection has been made.

let dateSelect = document.querySelector('[name="dayCount"]');
let clicked = false;

function changeClick(){
  clicked = true;
}

function checkFocus(){
  clicked === true ? console.log('BLUR FIRED -> select has lost focus') : null;
}

function mouseOut(){
  clicked === true ? console.log('MOUSEOUT FIRED -> Your mouse is not over the select element') : null;
}

dateSelect.addEventListener('click', changeClick);

dateSelect.addEventListener('blur', checkFocus);  
  
dateSelect.addEventListener('mouseout', mouseOut);
<select name="dayCount">
  <option >op1</option>
  <option >op2</option>
  <option>op3</option>
</select>

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!