DOM Document.activeElement Property
The activeElement property returns the element that currently has keyboard focus.
Description
The read-only activeElement property returns the element that currently has focus, or the <body> (or null) if nothing is focused. It's useful for remembering which element opened a dialog, so focus can be returned to it when the dialog closes. Inside shadow DOM, the shadow root has its own activeElement.
For elements inside an iframe, the parent document's activeElement is the <iframe> itself. Watching the focusin and focusout events on the document, which bubble unlike focus and blur, is the easiest way to track focus changes across the page.
Checking activeElement is also handy for keyboard shortcuts, so they can be ignored while the user is typing in an <input>, <textarea> or contenteditable element.
Syntax
document.activeElementValue
The element that currently has keyboard focus, or <body> if nothing does.
Example
Browser Support
The following information will show you the current browser support for the Document.activeElement property. Hover over a browser icon to see the version that first introduced support for this property.
Desktop





Tablets & Mobile





