2

One of my web app's page consists of a grid and a search box. I capture the keypress event to select through each of the grid's rows using the space bar. However, this creates a problem for when someone wants to look up a phrase in the search box.

I know that jQuery has a .focus method to bind an event handler to an input or give focus to an element. But how do I get a boolean value to determine if the search input has focus or not?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Zishan Neno
  • 2,647
  • 7
  • 34
  • 58

3 Answers3

5

try this

if ($("#id").is(":focus")) {
  alert('focus');
}

you can find more info on below link :

Using jQuery to test if an input has focus

Community
  • 1
  • 1
Shreedhar
  • 5,502
  • 3
  • 22
  • 27
2

Like so?

if($('...').is(':focus')){

}
wanovak
  • 6,117
  • 25
  • 32
1

Perhaps:

$(this).is(":focus");
11684
  • 7,356
  • 12
  • 48
  • 71