activeElement
Summary
Gets the object that has the focus when the parent document has focus. If no element in the document has focus, returns the <body> element.
Property of dom/Documentdom/Document
Syntax
Note: This property is read-only.
var element = document.activeElement;
Return Value
Returns an object of type DOM NodeDOM Node
The currently active element of the document, or <body>
if no element is active.
Examples
//display tag name of currently focused element
function getActiveElement () {
if (document.activeElement) {
alert(document.activeElement.tagName);
}
}
Notes
The active element retains focus in the parent Document even when focus is shifted from the parent to another application. If the focus returns to the parent document, focus also returns to the same active element.
Related specifications
- WHATWG HTML
- Living Standard
- W3C HTML5
- Candidate Recommendation
- W3C HTML5
- Candidate Recommendation
Attributions
Microsoft Developer Network: [Windows Internet Explorer API reference Article]