Skip to content

Home

Select the focused DOM element with JavaScript

Finding the currently focused DOM element is trivial in modern CSS, using the :focus selector. You can also use it in JavaScript, in combination with Document.querySelector() to find the focused element. Yet, there's an even easier way to get the currently focused element in JavaScript, using the Document.activeElement property.

const focusedElement = document.activeElement;
// `focusedElement` is the currently focused element

Note that focusable elements vary depending on browser and operating system. Additionally, you should remember that focus and selection (i.e. content highlighting) are not the same thing.

More like this

Start typing a keyphrase to see matching snippets.