Using jQuery, I can test if an input field has focus like so:
if ($("...").is(":focus")) { ... }
How do I do that without using jQuery?
HTML DOM Document hasFocus() The hasFocus() method returns a true if the document (or any element in the document) has focus. Otherwise it returns false .
To check whether a specific element has focus, it's simpler: var input_focused = document. activeElement === input && document. hasFocus();
focus() Javascript focus() methods helps to highlight a HTML form element. It sets the element as an active element in the current document. In current documentation, focus can be applied to only one single element. The focus can be applied either to a text, a button, etc.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With