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

203
Views
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
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!