How to get the focussed element on a webpage using JavaScript?

February 7, 2021 - 1 min read

To get the focussed element in a webpage using JavaScript, you can use the activeElement property in the global document object.

For example, Consider you have a lot of anchor tags in your webpage and needed to find out which anchor tag has the current focus. The document.activeElement property will return a reference to the current focussed element (not limited to anchor tags).

// Get focussed element in a webpage ✨
const focussedElement = document.activeElement;


See this example live in JSBin.

Feel free to share if you found this useful 😃.