Document activeElement Property - Javascript DOM

Javascript examples for DOM:Document activeElement

Description

The activeElement property returns the currently focused element in the document.

This property is read-only.

Return Value

A reference to the element object that has focus

The following code shows how to get the currently focused element in the document:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body onclick="myFunction()">

<p>Click anywhere to display the active element.</p>
<input type="text" value="An input field">
<button>A Button</button>

<p id="demo"></p>

<script>
function myFunction() {//from   w w  w.  j  ava 2 s .c  om
    var x = document.activeElement.tagName;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials