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: 
```
if ($("#lastname").is(':focus')) {
    alert("hello");
}

```

[jQuery docs for `:focus`](http://api.jquery.com/focus-selector/)   

[jQuery docs for `.is()`](http://api.jquery.com/is/)

---

**Update:**
To show/hide an element when the field gains/loses focus use the `focusin` and `focusout` events handlers:

```
$(document).ready(function() {
    $('#lastname').focusin(function() {
        $(hidden-element).show();
    }).add(hidden-element).focusout(function() {
        if ( !$(hidden-element).is(':focus') ) {
            $(hidden-element).hide();
        }
    });
});
//where hidden-element is a reference to or selector for your hidden text field

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

```

Check with == , using = is an assignment