Question: > 
> **Possible Duplicate:**  
> 
> [Using jQuery to test if an input has focus](https://stackoverflow.com/questions/967096/using-jquery-to-test-if-an-input-has-focus) 
> 
> 
> 

I need to check if a particular textbox has the focus or not using jQuery. So my code is like this, but it is not working:

```
if (document.getElementById("lastname").focus = true) {
    alert("hello");
}

```

My use-case is this: I have a textbox which should be shown or hidden when the `#lastname` textbox gains or loses focus. However, if the `#lastname` textbox loses the focus to the other textbox, the latter should remain visible until it loses focus as well.

Any help will be appreciated.

Answer: 
```
$("#lastname").focus(function(){
    alert("lastname has focus");
});

```
You can try jQuery's [:focus](http://api.jquery.com/focus-selector/) selector. Or you can always add a class and check for that class.