34

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

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

How do I do that without using jQuery?

Simon Steinberger
  • 6,605
  • 5
  • 55
  • 97
  • 1
    possible duplicate of [Javascript detect if input is focused](http://stackoverflow.com/questions/17614844/javascript-detect-if-input-is-focused) – richardgirges Jun 08 '15 at 16:52

1 Answers1

52

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