HTML DOM activeElement Property
Last Updated :
30 Aug, 2024
The DOM activeElement property is used to return the currently active elements in the HTML document. This property is read-only. It gives the reference of a focused element object in the document.
Syntax:
document.activeElement
Return Value: A reference to the element object in the document that has a focus.
Example: In this example, we will use DOM activeElement property.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: green;
}
</style>
<script>
function GFG() {
// Get Active Element using activeElement
// property and assign it value equal to
// to its tag
let x = document.activeElement.tagName;
document.getElementById("geeks").innerHTML = x;
}
</script>
</head>
<body onclick="GFG()">
<h1>GeeksforGeeks</h1>
<h2>DOM activeElement property</h2>
<input type="text" placeholder="GeeksforGeeks">
<button>Submit</button>
<p id="geeks"></p>
</body>
</html>
Output:

Supported Browsers: The browser supported by DOM activeElement property are listed below:
HTML DOM activeElement Property – FAQs
Can document.activeElement return null?
Yes, it returns null if no element in the document has focus.
Can document.activeElement be used to check focus changes?
Yes, it can be used in event handlers to check which element has focus at a given time.
How does document.activeElement behave with iframes?
It returns the focused element within the current document, including iframes. For iframes, it refers to the focused element inside that iframe.
Can document.activeElement be used with blur() and focus() methods?
Yes, you can use it to determine which element to call focus() or blur() on, based on the currently focused element.
Does document.activeElement work with dynamically created elements?
Yes, it reflects the focus state of dynamically created elements as long as they are part of the document.