
To detect if an element has the focus, you use the read-only property activeElement
of the document
object:
const el = document.activeElement
o detect if an element has focus, you compare it with the document.activeElement
.
The following checks if the input text with the .username
class has the focus:
const el = document.querySelector('.username');
console.log(el === document.activeElement);