Possible Duplicate:
How do I find out which DOM element has the focus?
Is there a way in javascript to determine which html page element has focus?
Syntax: var ele = document. activeElement; Return value: It returns the currently focused element in the document.
To detect if the element has the focus in JavaScript, you can use the read-only property activeElement of the document object. const elem = document. activeElement; The activeElement returns the currently focused element in the document.
In HTML document, the document. hasfocus() the method is used for indicating whether an element or document has the focus or not. The function returns a true value if the element is focused otherwise false is returned. This method can be used to determine whether the active element is currently in focus.
There isn't a definite list, it's up to the browser. The only standard we have is DOM Level 2 HTML, according to which the only elements that have a focus() method are HTMLInputElement , HTMLSelectElement , HTMLTextAreaElement and HTMLAnchorElement .
Use the document.activeElement
property.
The document.activeElement
property is supported on Chrome 2+, Firefox 3+, IE4+, Opera 9.6+ and Safari 4+.
Note that this property will only contain elements that accept keystrokes (such as form elements).
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