Check if an input field has focus in vanilla JavaScript

44.5k views Asked by At

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

There are 1 answers

1
richardgirges On BEST ANSWER

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