JavaScript Element blur Event

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

What You’ll Learn

The blur event fires when an element loses focus. Learn addEventListener vs onblur, why blur does not bubble, how focusout and capture help with delegation, FocusEvent.relatedTarget, and five try-it labs.

01

Kind

Instance event

02

Type

FocusEvent

03

Bubbles?

No

04

Cancelable?

No

05

Handler

onblur

06

Status

Baseline widely available

Introduction

Focus means which control is ready for keyboard input. When the user tabs or clicks away from a field, that field blurs—it loses focus—and the blur event fires on that element.

The opposite event is focus (gains focus). A closely related bubbling event is focusout, which is often better for listening on a parent container.

💡
Beginner tip

Prefer addEventListener("blur", ...) on the element itself. If you need one listener for many children, use focusout or listen to blur in the capture phase.

Understanding blur

An instance event that answers: “Did this element just lose focus?”

  • Fires when the element loses focus (click/tab away, hide, or remove in some browsers).
  • Does not bubble — use focusout or capture for delegation.
  • Not cancelable — you cannot stop focus from leaving via blur.
  • FocusEvent — check relatedTarget for the new focus target.
  • Baseline Widely available on MDN (since July 2015).

📝 Syntax

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

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

onblur = (event) => { };

Event type

A FocusEvent (inherits from UIEvent and Event).

Useful property

PropertyMeaning
relatedTargetThe element receiving focus, if any

⚖️ blur vs focusout vs focus

EventWhenBubbles?
blurElement lost focusNo
focusoutElement lost focus (follows blur)Yes
focusElement gained focusNo
focusinElement gained focusYes

🔗 Event Delegation Options

MDN notes two ways to delegate when blur itself does not bubble:

  1. Listen for focusout on a parent (it bubbles).
  2. Listen for blur with addEventListener(..., true) (capture phase).

⚡ Quick Reference

GoalCode / note
Listen on elementel.addEventListener("blur", fn)
Handler propertyel.onblur = fn
Delegate (bubble)parent.addEventListener("focusout", fn)
Delegate (capture)parent.addEventListener("blur", fn, true)
See next focusevent.relatedTarget
MDN statusBaseline Widely available (Jul 2015)

🔍 At a Glance

Four facts to remember about blur.

Event type
FocusEvent

relatedTarget

Bubbles
no

Use focusout

Cancelable
no

Cannot block leave

Baseline
yes

Widely available

Examples Gallery

Examples follow MDN Element: blur event. Click or tab between fields in the Try It Yourself labs to see focus and blur.

📚 Getting Started

Highlight a field on focus and clear the style on blur (MDN simple example).

Example 1 — focus + blur Styles

MDN idea: pink background while focused; reset when the field blurs.

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

focus paints the active field. blur runs when focus leaves and restores the default look.

Example 2 — onblur Property

Same leave-focus hook using the handler property.

JavaScript
const input = document.querySelector("input");

input.onblur = () => {
  console.log("Field lost focus");
};

// Assigning again replaces the previous onblur handler.
Try It Yourself

How It Works

Prefer addEventListener when you need multiple listeners or easy removal.

📈 Capture, relatedTarget & focusout

Delegate from a form, inspect where focus went, and compare with focusout.

Example 3 — Delegation with Capture

MDN pattern: listen on the form with useCapture = true.

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

Capture runs on the way down the tree, so the parent hears child focus/blur even though those events do not bubble.

Example 5 — Prefer focusout for Bubbling

Delegate leave-focus handling with the bubbling sibling of blur.

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

form.addEventListener("focusout", (event) => {
  console.log("focusout from:", event.target.name || event.target.tagName);
  if (event.target.matches("input")) {
    event.target.style.background = "";
  }
});
Try It Yourself

How It Works

focusout follows blur and bubbles, so one parent listener can clean up many fields without capture mode.

🚀 Common Use Cases

  • Clear highlight styles when a field is no longer active.
  • Validate a control when the user leaves it (often with focusout).
  • Save draft values when an editor loses focus.
  • Hide floating UI (tooltips, suggestion lists) on blur.
  • Log focus movement with relatedTarget for accessibility debugging.

🔧 How It Works

1

Element has focus

User is typing or the control is the active element.

Focused
2

Focus moves away

Click, Tab, hide, or (in some browsers) remove the element.

Leave
3

blur fires

Non-bubbling FocusEvent on the element that lost focus.

Event
4

focusout may follow

Bubbling sibling—great for parent-level listeners.

📝 Notes

  • MDN: Baseline Widely available (since July 2015).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Does not bubble; not cancelable.
  • Document.activeElement during blur handling can differ across browsers.
  • Related learning: beforexrselect, Window blur, Window focus, JavaScript hub.

Universal Browser Support

blur is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project. Still watch small differences when removing a focused node from the document.

Baseline · Widely available

Element blur

Works across modern desktop and mobile browsers when an element loses focus.

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
blur Baseline

Bottom line: Listen on the element for blur, or use focusout / capture when you need parent-level delegation.

Conclusion

blur is the standard “this element just lost focus” signal. Pair it with focus for enter/leave styling, use relatedTarget when you care where focus went, and reach for focusout or capture when you need delegation.

Continue with click, Window focus, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Prefer addEventListener("blur", ...) on the element
  • Use focusout for parent-level leave-focus logic
  • Pair with focus / focusin for enter/leave UI
  • Read relatedTarget when tracking focus movement
  • Test keyboard Tab paths, not only mouse clicks

❌ Don’t

  • Expect blur to bubble like click
  • Call preventDefault() hoping to keep focus
  • Assume removing a focused node always fires blur (Firefox differs)
  • Confuse Element blur with Window blur
  • Overwrite onblur if you need multiple listeners

Key Takeaways

Knowledge Unlocked

Five things to remember about blur

Focus left this element—style, validate, or clean up.

5
Core concepts
📄 02

FocusEvent

relatedTarget

API
🚫 03

No bubble

use focusout

Caveat
🛑 04

Not cancelable

cannot block leave

Rule
🎯 05

Baseline

widely available

Compat

❓ Frequently Asked Questions

The blur event fires when an element has lost focus. The opposite event is focus, which fires when an element receives focus.
No. MDN marks Element blur as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
No. blur does not bubble. The related focusout event that follows does bubble. For event delegation you can use focusout, or listen to blur with useCapture set to true.
No. The blur event is not cancelable. You cannot prevent focus from leaving with preventDefault() on blur.
A FocusEvent (inherits from UIEvent). A useful property is relatedTarget — the element receiving focus, if any.
Besides clicking or tabbing elsewhere, focus can leave if a style that prevents focus is applied (for example hidden), or if the element is removed from the document. Browser behavior differs when a focused element is removed: Chromium often fires blur; Firefox may not.
Did you know?

While a blur handler runs, document.activeElement is not the same in every browser. Some engines briefly point at body; older IE pointed at the element that will receive focus. Prefer event.target and event.relatedTarget inside the handler.

Next: click

Learn the everyday primary-activation event for elements.

click →

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