How do I find out which DOM element has the focus?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Knowing which DOM element currently has focus is essential for building accessible forms, keyboard navigation, custom focus indicators, and debugging focus-related issues. The document.activeElement property returns the currently focused element at any point in time. Combined with focus and blur events, it gives you complete control over focus management in web applications.
Basic: document.activeElement
When no interactive element has focus (e.g., the user clicked on empty space), document.activeElement returns the <body> element. If the document has not finished loading or has no focus at all, it may return null.
Checking Focus State
Listening for Focus Changes
focus and blur Events
focusin and focusout (Bubble)
Unlike focus/blur, focusin/focusout bubble up the DOM tree, making them ideal for event delegation:
e.relatedTarget in focusout tells you which element is receiving focus next (or null if focus is leaving the document).
Focus in Shadow DOM
When using Web Components with Shadow DOM, document.activeElement returns the shadow host, not the focused element inside the shadow tree:
Focus Management Patterns
Trap Focus in a Modal
Restore Focus After Modal Close
Skip Links for Keyboard Navigation
jQuery Equivalent
Debugging Focus Issues
Common Pitfalls
document.activeElementreturns<body>: This happens when no interactive element has focus. Checkdocument.activeElement !== document.bodybefore using the result, or verify the element type.- Focus during page load:
document.activeElementmay returnnullor<body>before the DOM is fully loaded. Wait forDOMContentLoadedbefore querying focus state. focus/blurdo not bubble: If you need event delegation (listening on a parent), usefocusin/focusoutinstead. This is a common source of "my focus handler never fires" bugs.relatedTargetis null: Infocusout,e.relatedTargetisnullwhen focus moves outside the document (e.g., to the address bar or another application). Handle this case explicitly.- Programmatic focus requires user interaction context: Some browsers restrict
element.focus()calls that are not triggered by user interaction (e.g., calling.focus()in asetTimeoutafter page load). This is especially strict on mobile browsers.
Summary
- Use
document.activeElementto get the currently focused DOM element at any time - Use
focusin/focusoutevents (notfocus/blur) for event delegation on parent containers - Use
e.relatedTargetinfocusoutto determine where focus is moving next - For Shadow DOM, walk
shadowRoot.activeElementrecursively to find the deeply focused element - Always restore focus to the previously focused element when closing modals or dialogs
Related reading
- How do I fix wrongly nested / unclosed HTML tags?
- How do I force a favicon refresh?
- How do I get started with Node.js
- How do I get the path to the current script with Node.js?
- How do I get the value of text input field using JavaScript?
- How do I integrate Ajax with Django applications?
- How do I know I've hit the threads limit defined in Node?
- How do I make multiple async calls, then execute a function after all calls have completed?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.