To detect if an element has the focus, you use the read-only property activeElement of the document object:
const el = document.activeElementCode language: JavaScript (javascript)To 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);Code language: JavaScript (javascript)Was this tutorial helpful ?