33

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

51

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
1
  • 4
    Example of use: if (document.activeElement.tagName === "INPUT") console.log("An input is focused");
    – Dror Bar
    Feb 7, 2021 at 13:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.