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

103
Visualizações
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 Respostas
Responde à pergunta

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 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