How to determine which html page element has focus?
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).
How do I find out which DOM element has the focus?
Use document.activeElement
, it is supported in all major browsers.
Previously, if you were trying to find out what form field has focus, you could not. To emulate detection within older browsers, add a "focus" event handler to all fields and record the last-focused field in a variable. Add a "blur" handler to clear the variable upon a blur event for the last-focused field.
If you need to remove the activeElement
you can use blur; document.activeElement.blur()
. It will change the activeElement
to body
.
Related links:
- activeElement Browser Compatibility
- jQuery alternative for document.activeElement
Determining which element has focus
Duplicate:
- How to determine which html page element has focus?
- How do I find out which Javascript element has focus?
Long story short:
Some browsers (originally only IE, but Firefox 3 and Safari jumped on the wagon) support the document.activeElement
property, which achieves what you want.
For older browsers, you need this hack to emulate the property:
function _dom_trackActiveElement(evt) {
if (evt && evt.target) {
document.activeElement = evt.target == document ? null : evt.target;
}
}
function _dom_trackActiveElementLost(evt) {
document.activeElement = null;
}
if (!document.activeElement) {
document.addEventListener("focus",_dom_trackActiveElement,true);
document.addEventListener("blur",_dom_trackActiveElementLost,true);
}
How to determine where focus went?
You can try something like this:
function whereDidYouGo() {
var all = document.getElementsByTagName('*');
for (var i = 0; i < all.length; i++)
if (all[i] === all[i].ownerDocument.activeElement)
return all[i];
}
EDIT:
function whereDidYouGo() { return document.activeElement; }
How to select the element that has the focus?
var focusedElement = $(':focus');
Which HTML elements can receive 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 areHTMLInputElement
, HTMLSelectElement
, HTMLTextAreaElement
and HTMLAnchorElement
. This notably omits HTMLButtonElement
and HTMLAreaElement
.
Today's browsers define focus()
on HTMLElement, but an element won't actually take focus unless it's one of:
- HTMLAnchorElement/HTMLAreaElement with an href
- HTMLInputElement/HTMLSelectElement/HTMLTextAreaElement/HTMLButtonElement but not with
disabled
(IE actually gives you an error if you try), and file uploads have unusual behaviour for security reasons - HTMLIFrameElement (though focusing it doesn't do anything useful). Other embedding elements also, maybe, I haven't tested them all.
- Any element with a
tabindex
There are likely to be other subtle exceptions and additions to this behaviour depending on browser.
Related Topics
Settimeout and "This" in JavaScript
Script Tag in JavaScript String
How to Dodge Jquery Promises Completely When Chaining Two Async Jquery Functions
Alternation Operator Inside Square Brackets Does Not Work
How to Decode Jwt Token in JavaScript Without Using a Library
React-Router - Pass Props to Handler Component
How to Tell If Caps Lock Is on Using JavaScript
Remove All Elements Contained in Another Array
Add a "Hook" to All Ajax Requests on a Page
Array VS. Object Efficiency in JavaScript
How to Get a Word Under Cursor Using JavaScript
Getting Unparsed (Raw) HTML with JavaScript
How to Solve Uncaught Rangeerror When Download Large Size JSON
Change Text Color Based on Brightness of the Covered Background Area
Anyone Have a Diff Algorithm for Rendered HTML
JavaScript to Scroll Long Page to Div
How to Find a Parent with a Known Class in Jquery
Integrating Dropzone.Js into Existing HTML Form with Other Fields