The activeElement property is a read-only reference to the element in your document that is receiving keyboard events—usually the focused field or button. Learn how it differs from text selection, what MDN’s textarea demo shows, iframe and shadow-DOM edge cases, and five hands-on examples with try-it labs.
01
Kind
Instance property
02
Returns
Element
03
Means
Keyboard focus
04
vs
getSelection()
05
Move focus
element.focus()
06
Status
Baseline widely
Fundamentals
Introduction
When a user tabs into a search box or clicks a button, that element becomes the active (focused) element. Keyboard shortcuts, typing, and Space/Enter activation go to whatever has focus.
document.activeElement tells you which element that is right now. It is one of the most practical Document properties for forms, modals, keyboard shortcuts, and accessibility tooling.
💡
Focus is not selection
Highlighted text in a textarea is selection. Use window.getSelection() for that. activeElement answers “which control has the caret?”
Platform differences — on Safari/macOS, non-text controls may not be focusable until “Full Keyboard Access” is enabled in System Settings.
Cross-document focus — if focus is outside this document tree, the property may be null or point at a host such as an <iframe>.
Foundation
📝 Syntax
JavaScript
document.activeElement
Value
The deepest focused Element. If no element has focus, browsers typically fall back to document.body or document.documentElement.
Typical patterns
JavaScript
// Read who has focus
const el = document.activeElement;
console.log(el.tagName, el.id);
// Move focus programmatically
document.getElementById("email").focus();
console.log(document.activeElement.id); // "email"
// Guard before acting on keyboard shortcuts
if (document.activeElement.matches("input, textarea")) {
return; // do not steal keys while typing
}
Compare
⚖️ activeElement vs Related Ideas
Idea
Meaning
document.activeElement
Element receiving keyboard input (focus)
window.getSelection()
Highlighted text range in the document
element.focus()
Programmatically move focus to an element
element.blur()
Remove focus from an element
event.target
Element an event was dispatched to (click origin)
:focus CSS pseudo-class
Style the element that currently has focus
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Read focused element
document.activeElement
Check tag / id
document.activeElement.tagName
Move focus
element.focus()
Is it an input?
activeElement.matches("input, textarea, select")
Text selection
window.getSelection() (not activeElement)
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance
Four facts to remember about document.activeElement.
You cannot assign document.activeElement. Call focus() on the element you want active, then read the property to verify.
Applications
🚀 Common Use Cases
Form UX — auto-focus the first invalid field after validation fails.
Modals — trap focus inside a dialog and restore focus on close.
Keyboard shortcuts — skip global handlers when the user is typing in an input.
Rich text / textarea tools — apply formatting to the focused field (MDN pattern).
Accessibility audits — log which control receives Tab order.
SPA routing — move focus to main content after navigation for screen readers.
Under the Hood
🔧 How Focus Tracking Works
1
User or script moves focus
Tab, click, or element.focus() targets a focusable element.
Input
2
Browser updates active element
document.activeElement now references that node.
State
3
Keyboard events route here
Typing and Space/Enter activation go to the active element.
Events
4
✓
Your code reads it
Branch on tag, id, or matches() to build keyboard-friendly UI.
Important
📝 Notes
MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
Read-only — use element.focus() to change focus.
Focus ≠ selection — pair with getSelection() when you need highlighted text.
Shadow DOM / iframe trees may return a host element or null across document boundaries.
Safari on macOS may limit focus on non-text elements unless Full Keyboard Access is on.
Also exists on ShadowRoot.activeElement for shadow trees.
Compatibility
Universal Browser Support
Document.activeElement is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
Document.activeElement
Read-only Element reference for keyboard focus — essential for forms, modals, shortcuts, and accessibility.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported in legacy IE
Full support
Document.activeElementExcellent
Bottom line: Use document.activeElement to read focus, element.focus() to move it, and getSelection() when you need highlighted text — not the same thing.
Wrap Up
Conclusion
document.activeElement is the standard way to ask “which element has focus right now?” Use it for forms, keyboard shortcuts, and focus management — and remember that text selection is a separate concept.
Read document.activeElement when handling global keys
Call focus() to move focus programmatically
Use focusin / focusout for bubbling focus tracking
Restore focus after closing modals (save previous element first)
Guard shortcuts when activeElement is an editable field
❌ Don’t
Assign to document.activeElement
Confuse focus with getSelection() highlighted text
Assume every clickable element is focusable on every platform
Steal focus on every route change without an accessibility reason
Read activeElement from the wrong document (iframe vs parent)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about activeElement
The read-only focus pointer on every Document.
5
Core concepts
🎯01
Focus
keyboard target
API
📝02
Read-only
use focus()
Pattern
📋03
Selection
different API
Compare
🔍04
MDN demo
textarea id
Example
🎯05
Baseline
since Jul 2015
Status
❓ Frequently Asked Questions
It is a read-only Document property that returns the deepest Element currently receiving keyboard events (keydown, keyup, and similar). In most cases that is the same element that has focus.
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() 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 the 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 element) instead of the inner node.
Did you know?
The same property exists on shadow roots: shadowRoot.activeElement returns the focused element inside that shadow tree. On the light DOM document, focus inside closed or nested shadow trees may surface as the shadow host element instead of the inner node.