Favorites

DOM Document.activeElement Property

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Document.activeElement
DOM Document PropertyThis property is supported by all modern browsers.
Updated 28th November 2026

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.activeElement

Value

The element that currently has keyboard focus, or <body> if nothing does.

Example

<input id="a" placeholder="a">
<input id="b" placeholder="b">
const a = document.getElementById('a');
console.log(document.activeElement.tagName);
a.focus();
console.log(document.activeElement.id);
document.addEventListener('focusin', () => console.log('Focus is on', document.activeElement.id || document.activeElement.tagName));

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.

This property is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee