41

Using jQuery, I can test if an input field has focus like so:

if ($("...").is(":focus")) {
    ...
}

How do I do that without using jQuery?

1

1 Answer 1

60

This question was answered here: Javascript detect if input is focused

Taken from the above answer:

this === document.activeElement // where 'this' is a dom object
Sign up to request clarification or add additional context in comments.

1 Comment

Example of use: if (document.activeElement.tagName === "INPUT") console.log("An input is focused");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.