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 Selector
- jQuery | focus() with Examples
- How to select all on focus in input using JQuery?
- jQuery lose focus event
- 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
- How to change the element id using jQuery ?
- How to get all CSS styles associated with an element using jQuery ?
- jQuery | element Selector
- How to create a div element in jQuery ?
- How to get a DOM Element from a jQuery Selector ?
- jQuery | element + next Selector
- How to select an element by name with jQuery?
- How to get focused element using jQuery?
- jQuery | Create a div element
- How to make an element “flash” in 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 email@example.com. 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.