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 = document.activeElement.id; Returns: The id of the active element, if any.
Sample Code to demonstrate working:
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
- Before clicked/focused:
- After clicked/focused:
- When input field is clicked/focused
- jQuery | focus() with Examples
- jQuery | :focus Selector
- How to select all on focus in input using JQuery?
- JQuery | Set focus on a form input text field on page load
- How to find element with specific ID using jQuery ?
- How to remove parent element except its child element using jQuery ?
- jQuery | Move an element into another element
- jQuery | element Selector
- How to get a DOM Element from a jQuery Selector ?
- How to change the element id using jQuery ?
- jQuery | Create a div element
- How to get focused element using jQuery?
- How to select an element by name with jQuery?
- How to get all CSS styles associated with an element using jQuery ?
- How to create a div element in jQuery ?
- jQuery | element + next Selector
- How to check if an element is hidden in jQuery?
- How to scroll to specific element using jQuery ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.