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

208
Visualizações
How to hide a div when the input is out of focus?

I am trying to show the recent searches when an user clicks to the input form to type something. And hide it when the user clicks outside of the input box. I'm using Vue (v3.2.29).

I've got the first part(showing the div when user clicks the input box) working but cannot solve the second part of the problem.

Apparently there is no straightforward way in JavaScript to tell whether the DOM is in focus or not.

This what my function for this looks like:

const showRecentSearches = () => {
  if (document.getElementById("search-box") === document.activeElement) {
    return true;
  } else {
    return false;
  }
};

This works fine for showing the div when the input is in focus but fails to hide it when it is out of focus.

Edit: This code from a discussion on GitHub solves the problem with a custom directive for click-outside event.

about 4 years ago · Juan Pablo Isaza
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