The focus event fires when an element receives focus. Learn addEventListener vs onfocus, why it does not bubble, focusin / capture delegation, relatedTarget, the blur pair, and five try-it labs.
01
Kind
Instance event
02
Type
FocusEvent
03
Bubbles?
No (use focusin)
04
Handler
onfocus
05
Pair
blur
06
Status
Baseline widely available
Fundamentals
Introduction
Keyboard and assistive-tech users move through a page with focus. When an input, button, link, or other focusable control becomes the active element, the browser fires focus on that element.
Pair it with blur (focus lost) to highlight fields, show hints, start validation, or clean up styles. Remember: focus does not bubble, so a parent listener in the bubble phase will miss child focus changes unless you use capture or focusin.
💡
Beginner tip
Only focusable elements receive focus (form controls, links, elements with a meaningful tabindex). Clicking a plain div usually does nothing for focus unless you make it focusable on purpose.
Concept
Understanding focus
An instance event that answers: “Did this element just become the focused element?”
Fires when the element receives focus (click, Tab, element.focus()).
Does not bubble — use focusin or capture-phase listening for delegation.
Not cancelable — preventDefault() does not block focusing.
FocusEvent — relatedTarget is the element losing focus (if any).
Handler — onfocus or addEventListener("focus", ...).
Baseline Widely available on MDN.
Foundation
📝 Syntax
Use the event name with addEventListener, or set the handler property:
When focus moves from element A to element B, MDN documents this order:
blur on A
focusout on A (bubbles)
focus on B
focusin on B (bubbles)
So focus is the non-bubbling “this element gained focus” signal; focusin is the bubbling sibling that follows.
Compare
⚖️ focus vs focusin
Topic
focus
focusin
When
Element receives focus
Same moment (after focus)
Bubbles?
No
Yes
Best for
Listening on the element itself
Delegation on a parent
Opposite pair
blur
focusout
🔔
Delegation tip
Two portable options: listen for focusin on a parent, or call addEventListener("focus", handler, true) so the capture phase sees the event on the way down.
Each focused control inside the form gets a pink background; blur clears it.
How It Works
The third argument true means capture phase. Without it, the form would not see child focus events because they do not bubble. Alternatively use focusin / focusout.
Example 4 — Log relatedTarget
See which element lost focus when a new one gains it.
JavaScript
const a = document.getElementById("a");
const b = document.getElementById("b");
const out = document.getElementById("out");
function onFocus(event) {
const from = event.relatedTarget
? event.relatedTarget.id || event.relatedTarget.tagName
: "(none)";
out.textContent =
"focus on #" + event.target.id + " · relatedTarget=" + from;
}
a.addEventListener("focus", onFocus);
b.addEventListener("focus", onFocus);
Hint visible while email is focused; hidden after blur.
How It Works
Keep critical instructions visible for everyone (not only on focus). Use focus hints for extra tips that reduce clutter until the user engages the field.
Applications
🚀 Common Use Cases
Highlight the active form field with a CSS class or inline style.
Show contextual help text while a control is focused.
Start soft validation or formatting when the user enters a field.
Track focus changes for analytics or accessibility tooling demos.
Delegate focus styling across many inputs with capture or focusin.
Under the Hood
🔧 How It Works
1
User focuses a control
Tab, click, or element.focus().
Input
2
Previous element blurs
blur then focusout on the old target.
Leave
3
focus fires on the new element
Does not bubble; focusin follows and does bubble.
Event
4
✓
Your handler runs
Style, hint, log relatedTarget, then clean up on blur.
Important
📝 Notes
MDN: Baseline Widely available.
Not Deprecated, Experimental, or Non-standard — no status banner required.
focus is marked Baseline Widely available on MDN. Logos use the shared browser-image-sprite.png sprite from this project. Focus behavior is core to forms and accessibility across engines.
✓ Baseline · Widely available
Element focus
Works across modern browsers for keyboard and pointer focus on focusable elements.
100%Widely available
Google ChromeSupported · Desktop & Android
Full support
Mozilla FirefoxSupported · Desktop & Android
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
OperaSupported · Modern versions
Full support
Internet ExplorerSupported (legacy)
Supported
focusBaseline
Bottom line: Use focus/blur on the element itself; use focusin or capture for parent delegation.
Wrap Up
Conclusion
focus is the “this element received focus” signal. Pair it with blur, remember it does not bubble, and reach for focusin or capture when you need one listener for many controls.
Force focus on non-interactive elements without a good reason
Overwrite onfocus if you need multiple listeners
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about focus
Element gained focus—FocusEvent; does not bubble; pair with blur.
5
Core concepts
💡01
Got focus
Tab / click / .focus()
Trigger
📄02
FocusEvent
relatedTarget
API
🚫03
No bubble
use focusin / capture
Limit
⇄04
Pair with blur
clean up styles
Pattern
🎯05
Baseline
widely available
Status
❓ Frequently Asked Questions
It fires when an element receives focus — for example when a user tabs to an input or clicks into it. The opposite event is blur, which fires when focus is lost.
No. MDN marks Element focus as Baseline Widely available. It is not Deprecated, Experimental, or Non-standard.
No. focus does not bubble. The related focusin event does bubble. For event delegation you can use focusin, or listen for focus in the capture phase (addEventListener(..., true)).
A FocusEvent (inherits from UIEvent and Event). The relatedTarget property is the element that is losing focus, if any.
No. The focus event is not cancelable. You cannot stop an element from receiving focus by calling preventDefault() on focus.
Use focusin when you want a single listener on a parent (form, dialog, page) to react to focus changes on descendants, because focusin bubbles.
Did you know?
When focus shifts from A to B, the order is blur → focusout → focus → focusin. That is why cleanup on A finishes before setup on B begins.