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

113
Views
Can't focus with jQuery?

I'm trying to focus on input when the search icon is clicked so the user can start typing without having to click on the input manually. Ideally, it should be something like this, but focus() doesn't work on this input for some reason.

$('.header__icons a[role="button"]').click(function(){
     $('#q').focus();
})

I've tried various options, but nothing..I see this input gets "focus-visible" and "data-focus-visible-added" when clicked on. I tried adding them manually, but it doesn't focus it either. I also couldn't locate any functions related to this in the files.

EDIT: The code works fine, just added setTimeout..and dev console was the reason I wasn't seeing any changes here :)

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

0

use setTimeout to wait animation complete

$('.header__icons a[role="button"]').click(function() {
  setTimeout(function() {
    $('#q').focus();
  }, 1000)
})

about 4 years ago · Juan Pablo Isaza Report

0

Actually, the code you have should work. I recommend you close the browser's developer console and test it again. The reason it probably doesn't work is that focus() doesn't steal focus from the dev console.

Try using a setTimeout in the dev console and then quickly click in your browser window, then you should see it focus the search box.

setTimeout(function() { $('input[name="q"]').focus() }, 3000);
about 4 years ago · Juan Pablo Isaza Report

0

The problem is that when the user clicks the button, the field is not yet visible, so it cannot be focused. It will be visible in the next tick though (doesn't matter that it's transparent - it's about whether it's rendered at all or not), so you can use setTimeout with a timeout of zero:

$('.header__icons a[role="button"]').click(function(){
     setTimeout(() => $('#q').focus(), 0)
})
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!