JavaScript Element focusin Event

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
DOM event
Instance event

What You’ll Learn

The focusin event fires after focus when an element receives focus—and unlike focus, it bubbles. Learn onfocusin, form delegation, relatedTarget, the focusout pair, and five try-it labs.

01

Kind

Instance event

02

Type

FocusEvent

03

Bubbles?

Yes

04

Handler

onfocusin

05

Pair

focusout

06

Status

Limited availability

Introduction

focus is perfect when you attach a listener directly to one control. When you want one listener on a form or dialog to react to every child that gains focus, focusin is the event designed for that job because it bubbles.

It fires after focus. Its opposite is focusout (which bubbles after blur). MDN marks focusin as Limited availability (not Baseline)—still widely useful, but always verify support for your audience.

💡
Beginner tip

If a parent focus listener never runs for child inputs, that is expected—focus does not bubble. Switch to focusin (or listen for focus in the capture phase).

Understanding focusin

An instance event that answers: “Did this element (or a descendant) just receive focus?” when you listen on an ancestor.

  • Fires after focus when an element receives focus.
  • Bubbles — ideal for event delegation on forms and containers.
  • Not cancelablepreventDefault() does not block focusing.
  • FocusEventrelatedTarget is the element losing focus (if any).
  • Handleronfocusin or addEventListener("focusin", ...).
  • Limited availability on MDN (not Baseline)—test carefully.

📝 Syntax

Use the event name with addEventListener, or set the handler property:

JavaScript
addEventListener("focusin", (event) => { });

onfocusin = (event) => { };

Event type

A FocusEvent (inherits from UIEvent and Event).

Handy properties

PropertyMeaning
targetThe element receiving focus (often a child of your listener)
currentTargetThe element you attached the listener to (e.g. the form)
relatedTargetThe element losing focus, if any (may be null)
bubblestrue for focusin
cancelablefalse — not cancelable

🔁 Event Order (A → B)

When focus moves from element A to element B, browsers typically dispatch:

  1. blur on A
  2. focusout on A (bubbles)
  3. focus on B
  4. focusin on B (bubbles)

MDN notes that the UI Events specification describes an order that can differ from what current browsers implement—write code that does not depend on fragile interleaving details beyond this common pattern.

⚖️ focusin vs focus

Topicfocusfocusin
WhenElement receives focusSame moment, after focus
Bubbles?NoYes
Best forListener on the element itselfDelegation on a parent
Opposite pairblurfocusout
MDN statusBaseline Widely availableLimited availability (not Baseline)
🔔
Fallback idea

If you must support environments where focusin is awkward, you can still delegate with addEventListener("focus", handler, true) (capture phase). Prefer focusin when it is available and clearer.

⚡ Quick Reference

GoalCode / note
Listen on one controlel.addEventListener("focusin", fn)
Handler propertyel.onfocusin = fn
Delegate on a formform.addEventListener("focusin", fn)
Clear stylesPair with focusout
Previous focusevent.relatedTarget (may be null)
MDN statusLimited availability (not Baseline)

🔍 At a Glance

Four facts to remember about focusin.

Event type
FocusEvent

After focus

Means
got focus

Bubbling signal

Bubbles
yes

Great for forms

Baseline
no

Limited availability

Examples Gallery

Examples follow MDN Element: focusin event. Tab or click into fields inside the demos.

📚 Getting Started

MDN form styling and the onfocusin property.

Example 1 — Form Delegation (MDN)

One listener on the form highlights whichever child control gains focus.

JavaScript
const form = document.getElementById("form");

form.addEventListener("focusin", (event) => {
  event.target.style.background = "pink";
});

form.addEventListener("focusout", (event) => {
  event.target.style.background = "";
});
Try It Yourself

How It Works

Because focusin / focusout bubble, you do not need a separate listener on every input. Always clear styles on focusout.

Example 2 — onfocusin Property

Same delegation idea using the handler property form.

JavaScript
const form = document.getElementById("form");
const out = document.getElementById("out");

form.onfocusin = (event) => {
  out.textContent = "focusin on " + (event.target.name || event.target.tagName);
};

form.onfocusout = () => {
  out.textContent = "(no field focused)";
};
Try It Yourself

How It Works

Prefer addEventListener when you need multiple listeners. The property form is fine for short demos.

📈 Compare, relatedTarget & UX

Prove bubbling, inspect previous focus, track the active field.

Example 3 — Prove focus Does Not Bubble

Parent listeners: focusin sees children; bubble-phase focus does not.

JavaScript
const form = document.getElementById("form");
const out = document.getElementById("out");

form.addEventListener("focus", () => {
  out.textContent += "parent focus (bubble)\n";
});

form.addEventListener("focusin", (event) => {
  out.textContent +=
    "parent focusin → " + (event.target.name || event.target.tagName) + "\n";
});
Try It Yourself

How It Works

This is the classic interview demo. Capture-phase focus would still see children; bubble-phase focus on the form will not.

Example 5 — Track the Active Field

Show which control is currently focused using a single bubbling listener.

JavaScript
const form = document.getElementById("form");
const status = document.getElementById("status");

form.addEventListener("focusin", (event) => {
  if (!(event.target instanceof HTMLInputElement)) return;
  status.textContent = "Active: " + (event.target.name || event.target.type);
});

form.addEventListener("focusout", () => {
  status.textContent = "Active: (none)";
});
Try It Yourself

How It Works

Filter with instanceof (or matches) so clicks on labels or the form itself do not confuse your status text.

🚀 Common Use Cases

  • Highlight any focused control inside a form with one listener.
  • Show field-level help based on event.target.
  • Track the active field for analytics or accessibility demos.
  • Style an entire card / step when focus enters its subtree.
  • Teach event bubbling vs the non-bubbling focus event.

🔧 How It Works

1

User focuses a control

Tab, click, or element.focus().

Input
2

focus fires on the target

Does not bubble to ancestors in the bubble phase.

Target
3

focusin fires and bubbles

Parent form / dialog listeners can react.

Bubble
4

Clean up on focusout

Remove styles / hints when focus leaves the control.

📝 Notes

  • MDN: Limited availability (not Baseline)—test the browsers you support.
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • focusin bubbles; focus does not.
  • The event is not cancelable.
  • Related learning: focus, focusout, addEventListener(), JavaScript hub.

Limited Availability Support

focusin is marked Limited availability on MDN (not Baseline). Logos use the shared browser-image-sprite.png sprite from this project. It is widely used for form delegation—still verify support for your audience, and keep capture-phase focus as a mental fallback.

Limited availability

Element focusin

Bubbling focus signal for parent listeners; confirm support in your target browsers.

Limited Not Baseline
Google Chrome Supported (check BCD for edge cases)
Supported
Mozilla Firefox Supported in modern versions
Supported
Apple Safari Supported in modern versions
Supported
Microsoft Edge Supported · Chromium
Supported
Opera Supported · Modern versions
Supported
Internet Explorer Legacy support existed; prefer modern browsers
Legacy
focusin Limited

Bottom line: Prefer focusin for form delegation when available; pair with focusout and test carefully.

Conclusion

focusin is the bubbling “received focus” signal that follows focus. Use it for form-wide listeners, pair it with focusout, and remember MDN lists it as Limited availability.

Continue with focusout, addEventListener(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Prefer addEventListener("focusin", ...) on parents for delegation
  • Clear styles / hints with focusout
  • Filter event.target when you only care about inputs
  • Null-check event.relatedTarget
  • Test browsers you support (Limited availability)

❌ Don’t

  • Expect bubble-phase focus on a form to see child fields
  • Rely on preventDefault() to block focusing
  • Leave focus styles stuck after focusout
  • Assume Baseline Widely available status
  • Overwrite onfocusin if you need multiple listeners

Key Takeaways

Knowledge Unlocked

Five things to remember about focusin

After focus—bubbles; ideal for form delegation with focusout.

5
Core concepts
📄 02

FocusEvent

relatedTarget

API
📤 03

Bubbles

unlike focus

Key
04

Pair focusout

clean up styles

Pattern
🎯 05

Limited avail.

not Baseline

Status

❓ Frequently Asked Questions

It fires when an element has received focus, after the focus event. Unlike focus, focusin bubbles, so you can listen on a parent and still hear when child controls gain focus.
No. MDN does not mark it Deprecated, Experimental, or Non-standard. It has Limited availability (not Baseline), so always test the browsers you care about.
Both fire when an element receives focus. focus does not bubble; focusin does, and it fires after focus. For form-wide listeners, focusin is usually the simpler choice.
A FocusEvent (inherits from UIEvent and Event). relatedTarget is the element losing focus, if any.
No. The focusin event is not cancelable.
focusout — it fires when the element loses focus (after blur) and also bubbles.
Did you know?

MDN’s live example listens for both focusin and focusout on a single <form>. That one pair of listeners styles every text and password field inside—no per-input wiring required.

Next: focusout

Learn the bubbling blur event—perfect for cleanup and validate-on-leave.

focusout →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful