The Wayback Machine - https://web.archive.org/web/20240621180633/https://www.geeksforgeeks.org/how-to-find-which-dom-element-has-the-focus-using-jquery/
Open In App

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

Last Updated : 21 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Syntax:

let ele = document.activeElement;

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

Example Code Snippets:

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

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

Sample Code to demonstrate working: 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <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>
 
    <script>
        $("body").click(function () {
            let x = document.activeElement.tagName;
            document.getElementById("fun").innerHTML = x;
        });
    </script>
</body>
   
</html>


Explanation: The script inside the 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 that is returned by the

document.activeElement.tagName

Output:

 


Similar Reads

How to get the class of a element which has fired an event using JavaScript/JQuery?
One can use Javascript and JQuery to retrieve the class of an element that triggers an event. By capturing the event and accessing the class attribute, developers can dynamically identify the specific element involved in the interaction. Below are the methods to get the class of an element that has fired an event: Table of Content Using JavaScriptU
3 min read
How to select all on focus in input using JQuery?
The select() event is applied to an element using jQuery when the user makes a text selection inside an element or on focus the element. This event is limited to some fields. Syntax: $("selected element").select(); //Select all Input field on focus $("input").select(); //Select Input field on focus whose type="text" $("input[type='text']").select()
1 min read
How to prevent a text field losing focus using jQuery ?
In this article, we will learn how to prevent a textfield or an input from losing focus using jQuery. This can be used in situations where user validation is required. There are two approaches that can be taken: Approach 1: We will be using the jQuery bind(), val(), preventDefault() and focus() methods. The bind() method is used to attach a "focuso
3 min read
Set the focus to HTML form element using JavaScript
To set focus to an HTML form element, there can be many methods that can be used to focus. In this article, we will discuss them all. These are the following approaches: Table of Content Using focus() methodUsing window.onload methodApproach 1: Using focus() methodIn this approach, we are going to use the focus() method to focus on the HTML element
2 min read
HTML DOM focus() Method
DOM focus() method is used to give focus to an element and also to remove the focus with the help of blur() method. We can apply focus to any element and enable it by doing some operation. For example, we can give focus to some text by clicking a button. Syntax: Object.focus() Example-1: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt;
1 min read
HTML DOM Input Text focus() Method
The Input Text focus() method in HTML DOM is used to get focus to the text field when the event occurs. Syntax: textObject.focus() Parameters: This method does not contain any parameter values. Return Value: It does not return any value. Example: Below program illustrates the use of the input text focus() method in HTML DOM. C/C++ Code &lt;!DOCTYPE
1 min read
HTML DOM Input Search focus() Method
The Input Search focus() Method in HTML DOM is used to get focus to the Input search Field when an event occurs. Syntax: searchObject.focus();Parameter: This method does not contain any parameter value. Return Value: It does not return any value. Example: The below example illustrates the use of the Input search focus() method in HTML DOM. C/C++ Co
1 min read
HTML DOM Input URL focus() Method
The Input URL focus() method in HTML DOM is used to get focus to the Input URL field when the event occurs. We can use this method for directing the focus of the user when an event occurs. Syntax: URLObject.focus() Parameters: This method does not contain any parameter values. Return Value: It does not return any value. Example: Below program illus
1 min read
HTML DOM Input Tel focus() Method
The input tel focus() method in HTML DOM is used to get focus to an input tel field when the event occurs. Syntax: telObject.focus() Parameters: This method does not accept any parameter values. Return Value: This method does not return any values. Example: Below HTML code illustrates the use of the input tel focus() method. C/C++ Code &lt;!DOCTYPE
1 min read
How to check an element has certain CSS style using jQuery ?
Given an HTML document containing some CSS property and the task is to check whether an element has a specific CSS style or not with the help of jQuery. Approach 1: Use css() method to check an element contains certain CSS styles or not. Example: This example uses css() method to check an element contains certain CSS style or not. &lt;!DOCTYPE HTML
2 min read
three90RightbarBannerImg