-2

Possible Duplicates:
How to get input has focus or not using jquery
Using jQuery to test if an input has focus

Hi i want to test Input tag has focus or not using jquery

if(document.getElementById('Department').focus())

I am using. but it will not work it will always give me false. Please help me to solve this

Community
  • 1
  • 1
Royal Pinto
  • 2,869
  • 8
  • 27
  • 39
  • 3
    Didn't you already ask this earlier today? http://stackoverflow.com/questions/6703862/how-to-get-input-has-focus-or-not-using-jquery – marto Jul 15 '11 at 10:40
  • if you're using jQuery you can use `$('#Department')` instead of `document.getElementById('Department')` see http://api.jquery.com/id-selector/ and the other selectors. – StuperUser Jul 15 '11 at 10:40
  • Yes i have asked same question. – Royal Pinto Jul 15 '11 at 10:45
  • @Roayl: Your question is being closed *for a reason*. It's already been asked, and answered, here: http://stackoverflow.com/questions/967096/using-jquery-to-test-if-an-input-has-focus If the community closes your question, don't re-post it. See [the FAQ](http://stackoverflow.com/faq). – T.J. Crowder Jul 15 '11 at 10:50

2 Answers2

1

i think you may be able to use the following code

document.activeElement
Graham
  • 651
  • 2
  • 10
  • 23
  • No. I want to test multiple elements by using document.getElementById – Royal Pinto Jul 15 '11 at 10:49
  • Then you can test each one with *document.activeElement*. But only one (if any) can have focus at at time, so just get document.activeElement and see if it's one of the elements you wish to test using the id. – RobG Jul 15 '11 at 11:01
0
if($("#Department").is(":focus")) {
   //Do stuff
}

The :focus selector only matches elements that currently have focus. You can read more about it in the API. The is function tests the set of matched elements against a selector, and returns true if any of them match.

James Allardice
  • 164,175
  • 21
  • 332
  • 312