Document.activeElement is a read-only property that returns the deepest Element currently receiving keyboard events (such as keydown / keyup). That is usually the focused control (MDN).
Read the notes below before you get or set this property. Prefer feature detection when support is limited.
Syntax
Use document.activeElement. Start with this line.
JavaScript
document.activeElement
Call
What it does
document.activeElement
Primary signature.
Built into the browser. Read or assign document.activeElement — no import required.
Library declarations
JavaScript
document.activeElement
Parameters
Parameter
Description
focus() / Tab
Optional. How focus arrives on an element
Return Value
Return Value
Description
*
Focused Element (deepest in this document)
JavaScript Examples
Example 1 — Read the Focused Element
On focus, log tagName and id of activeElement.
JavaScript
const input = document.getElementById("search");
input.addEventListener("focus", function () {
const active = document.activeElement;
console.log(active.tagName); // "INPUT"
console.log(active.id); // "search"
});
Move focus.email.focus() makes it the keyboard target.
Verify with ===.activeElement should be the same object.
Do not assign. Writing to document.activeElement is not how you focus.
Compatibility
Browser Support
Document.activeElement is Baseline Widely available (MDN: since July 2015). Safe to use in modern and older evergreen browsers.
✓ Baseline · Widely available
Document.activeElement
Read-only focused Element — supported across modern browsers for years.
99%Widely available
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
activeElementExcellent
Bottom line: Safe for production. Pair with hasFocus() when you need to know if the document itself is focused, and remember focus rules can differ slightly by platform.
Frequently Asked Questions
A read-only Document property that returns the deepest Element currently receiving keyboard events (keydown, keyup, and similar). That is usually the focused element.
No. MDN marks Document.activeElement as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
When no element has focus, activeElement is usually document.body or document.documentElement — depending on the browser and page.
No. Focus is which element receives keyboard input. Selection is the highlighted text range. Use window.getSelection() (or document.getSelection()) for selection; use activeElement for focus.
No. It is read-only. To move focus, call element.focus() on the target you want active.
If focus is in another document (for example the main page while your script runs inside an iframe), activeElement from that iframe document may be null. If focus is inside a shadow tree hosted in your document, you may get the shadow host (such as an iframe) instead of the inner node.