How to find which DOM element has the focus using jQuery?

The HTML DOM has an activeElement Property. It can be used to get the currently focused element in the document:

Syntax: var ele = document.activeElement;

Return value: It returns the currently focused element in the document.
Example Code Snippets:

Example 1: var eleName = document.activeElement.tagName;
Returns: The Tag Name of the active element.

Example 2: var eleId =;
Returns: The id of the active element, if any.

Sample Code to demonstrate working:





<!DOCTYPE html>
    <script src=
    <p> Click wherever you like. 
The active/focused DOM name will be displayed at the end</p>
    <input type="text" value="Some input field">
    <button>Simple Button</button>
    <p id="fun"></p>
      $("body").click(function() {
        var x = document.activeElement.tagName;
        document.getElementById("fun").innerHTML = x;


Explanation of the above code:
The script inside body tag modifies the HTML Content of the DOM Element having id fun to the name of the DOM Element which is currently active or is in focus. This is the value which is returned by the



  1. Before clicked/focused:
  2. After clicked/focused:
  3. When input field is clicked/focused


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.