JavaScript Element focus Event

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

What You’ll Learn

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

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.

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 cancelablepreventDefault() does not block focusing.
  • FocusEventrelatedTarget is the element losing focus (if any).
  • Handleronfocus or addEventListener("focus", ...).
  • Baseline Widely available on MDN.

📝 Syntax

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

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

onfocus = (event) => { };

Event type

A FocusEvent (inherits from UIEvent and Event).

Handy properties

PropertyMeaning
targetThe element receiving focus
relatedTargetThe element losing focus, if any (may be null)
bubblesfalse for focus (unlike focusin)
cancelablefalse — not cancelable

🔁 Event Order (A → B)

When focus moves from element A to element B, MDN documents this order:

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

So focus is the non-bubbling “this element gained focus” signal; focusin is the bubbling sibling that follows.

⚖️ focus vs focusin

Topicfocusfocusin
WhenElement receives focusSame moment (after focus)
Bubbles?NoYes
Best forListening on the element itselfDelegation on a parent
Opposite pairblurfocusout
🔔
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.

⚡ Quick Reference

GoalCode / note
Listen on one controlel.addEventListener("focus", fn)
Handler propertyel.onfocus = fn
Style on focus / clear on blurSet style in focus; reset in blur
Delegate on a formfocusin or addEventListener("focus", fn, true)
Previous focusevent.relatedTarget (may be null)
MDN statusBaseline Widely available

🔍 At a Glance

Four facts to remember about focus.

Event type
FocusEvent

UIEvent + Event

Means
got focus

Element activated

Bubbles
no

Use focusin

Baseline
yes

Widely available

Examples Gallery

Examples follow MDN Element: focus event. Click or Tab into the fields to trigger focus.

📚 Getting Started

MDN-style styling and the onfocus property.

Example 1 — Highlight on focus, Clear on blur (MDN)

Change a password field background while it is focused.

JavaScript
const password = document.querySelector('input[type="password"]');

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

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

How It Works

Always undo focus styles in blur (or remove a CSS class) so the UI does not stay stuck in the “focused” look.

Example 2 — onfocus Property

Same idea using the handler property form.

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

name.onfocus = () => {
  out.textContent = "Name field focused";
};

name.onblur = () => {
  out.textContent = "Name field blurred";
};
Try It Yourself

How It Works

Prefer addEventListener when you need multiple listeners or easy removal. The property form is fine for tiny demos.

📈 Delegation, relatedTarget & UX

Capture-phase form listeners, previous focus, and helpful hints.

Example 3 — Capture-Phase Delegation (MDN)

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

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

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

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

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 5 — Show a Hint While Focused

Practical UX: reveal helper text only while the field has focus.

JavaScript
const email = document.getElementById("email");
const hint = document.getElementById("hint");

email.addEventListener("focus", () => {
  hint.hidden = false;
});

email.addEventListener("blur", () => {
  hint.hidden = true;
});
Try It Yourself

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.

🚀 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.

🔧 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.

📝 Notes

Universal Browser Support

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 Chrome Supported · Desktop & Android
Full support
Mozilla Firefox Supported · Desktop & Android
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Opera Supported · Modern versions
Full support
Internet Explorer Supported (legacy)
Supported
focus Baseline

Bottom line: Use focus/blur on the element itself; use focusin or capture for parent delegation.

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.

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

💡 Best Practices

✅ Do

  • Prefer addEventListener("focus", ...)
  • Always clear focus styles / hints on blur
  • Use focusin or capture for form-wide delegation
  • Null-check event.relatedTarget
  • Keep keyboard focus visible for accessibility

❌ Don’t

  • Expect focus to bubble like click
  • Rely on preventDefault() to block focusing
  • Leave focus styles stuck after blur
  • Force focus on non-interactive elements without a good reason
  • Overwrite onfocus if you need multiple listeners

Key Takeaways

Knowledge Unlocked

Five things to remember about focus

Element gained focus—FocusEvent; does not bubble; pair with blur.

5
Core concepts
📄 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 blurfocusoutfocusfocusin. That is why cleanup on A finishes before setup on B begins.

Next: focusin

Learn the bubbling focus event—perfect for one listener on a form.

focusin →

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