JavascriptJavaScript — Get the Currently Focused Element

JavaScript — Get the Currently Focused Element

In JavaScript, the currently active element is said to have Focus, which means it’s the element being acted upon. Like a form field in which you are typing is said to have Focus.

You can get the currently focused element with a simple reference on the Dom:

var currentElement = document.activeElement

This will return the element which has focus, or null if there is no focused element. It will let you have access to the whole element. You can get to any properties on that element like the Id or Name. The CodePen below is a simple example of using the document.activeElement

Example

See the Pen Get Focused Element by Chris (@cgreenws) on CodePen.

For more information:
https://www.w3schools.com/jsref/prop_document_activeelement.asp
or
https://developer.mozilla.org/en-US/docs/Web/API/DocumentOrShadowRoot/activeElement

Categories: Javascript Tags: , ,

Comments

No Comments Yet. Be the first?

Post a comment

Your email address will not be published. Required fields are marked *