Javascript detect if input is focused [duplicate]

Javascript detect if input is focused [duplicate]

This question already has an answer here:

Using jQuery to test if an input has focus

15 answers

How do I detect when .click event triggers if textarea is already focused?
I have this javascript:
$(“.status”).on(“click”,”textarea”,function(){
if ($(this) == “focused”) {
// fire this step
}else{
$(this).focus();
// fire this step
}

Solutions/Answers:

Solution 1:

With pure javascript:

this === document.activeElement // where 'this' is a dom object

or with jquery’s :focus pseudo selector.

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

Solution 2:

Did you try:

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

Take a look at Using jQuery to test if an input has focus it features some more examples

Solution 3:

If you can use JQuery, then using the JQuery :focus selector will do the needful

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

Solution 4:

Using jQuery’s .is( “:focus” )

$(".status").on("click","textarea",function(){
        if ($(this).is( ":focus" )) {
            // fire this step
        }else{
                    $(this).focus();
            // fire this step
    }