Zishan Neno
Zishan Neno

Reputation: 2817

Detect if input has focus

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?

Upvotes: 2

Views: 2201

Answers (3)

11684
11684

Reputation: 7507

Perhaps:

$(this).is(":focus");

Upvotes: 1

wanovak
wanovak

Reputation: 6127

Like so?

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

}

Upvotes: 2

Shreedhar
Shreedhar

Reputation: 5640

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

Upvotes: 5

Related Questions