HTML onblur Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onblur attribute handles the blur event. It runs when an element loses focus — for example after Tab or a click moves focus elsewhere.

The opposite is onfocus. blur does not bubble; use focusout (or capture) for parent-level listening. Prefer addEventListener("blur", …) in modern code.

Baseline · Widely available.

Stable across modern browsers since about July 2015. Not deprecated. The event is not cancelable.

Inline handlers discouraged.

MDN: prefer addEventListener over inline on* attributes. Study onblur to recognize it in markup.

HTML Syntax

syntax.html
<input onblur=" script">

script runs when the element loses focus. Works on focusable elements (inputs, buttons, links with href, etc.). Preferred: element.addEventListener("blur", handler).

Attribute Values

How you listen for blur:

Value / formDescriptionTry it
JavaScript on elementInline handler, e.g. checkEmail()Try it
addEventListener("blur")Preferred modern listenerTry it
focusout on parentBubbling alternative for delegationTry it

Live Preview

Focus the field, then Tab away or click elsewhere. Status updates when blur fires.

action

You can also Tab between the two inputs. blur fires on the field that just lost focus.

Demo

Waiting for blur…

HTML Examples

Short study snippets. Use Try It Yourself for a live editor. Prefer listeners in real apps.

Example 1 — Inline onblur on an input

Recognize the attribute form. Leaving the field updates status.

HTML
<label for="name">Name</label>
<input
  id="name"
  type="text"
  onblur="document.getElementById('status').textContent = 'Name lost focus: ' + this.value"
>
<p id="status">Focus the field, then Tab or click away.</p>
Try It Yourself

How It Works

  1. onblur registers an inline handler on the input.
  2. Focus the field (click or Tab).
  3. Leave with Tab or another click — blur fires.
  4. Status shows the value at the moment focus was lost.

Example 2 — Preferred addEventListener

Same event, cleaner HTML/script split. Trim the value on leave.

HTML
<label for="email">Email</label>
<input id="email" type="email" autocomplete="off">
<p id="status">Leave the field to trim spaces.</p>

<script>
  var email = document.getElementById("email");
  var status = document.getElementById("status");

  email.addEventListener("blur", function () {
    email.value = email.value.trim();
    status.textContent = "Blurred. Value: \"" + email.value + "\"";
  });
</script>
Try It Yourself

How It Works

  1. No attribute — the listener is attached in script.
  2. On blur the handler trims leading/trailing spaces.
  3. Status confirms the cleaned value.
  4. Typical use: light formatting or validation when leaving a field.

Example 3 — focusout on a parent (delegation)

blur does not bubble. focusout does — so one parent listener can watch many fields.

HTML
<form id="form">
  <label>First <input name="first" type="text"></label>
  <label>Last <input name="last" type="text"></label>
</form>
<p id="status">Leave any field inside the form.</p>

<script>
  var form = document.getElementById("form");
  var status = document.getElementById("status");

  form.addEventListener("focusout", function (event) {
    if (event.target.tagName === "INPUT") {
      status.textContent = "focusout from: " + event.target.name;
    }
  });
</script>
Try It Yourself

How It Works

  1. One listener on the form watches all nested inputs.
  2. focusout bubbles, so the parent receives the event.
  3. event.target is the field that lost focus.
  4. Alternative: listen for blur with capture (true) on the parent.

Edge Cases & Pitfalls

Most confusion comes from bubbling and cancelability.

bubble

blur does not bubble

A parent onblur will not see child blurs. Use focusout or capture-phase blur.

cancel

Not cancelable

You cannot stop focus from leaving with preventDefault() on blur.

pair

Pair with focus

focus runs on enter; blur on leave. Do not confuse with change (value committed) or input (each keystroke).

remove

Removing a focused element

Chromium often fires blur when a focused node is removed; Firefox may not. Prefer explicit UX over relying on that.

style

Prefer listeners

Inline on* attributes mix concerns. Use addEventListener in production code.

Browser Support

onblur is a global event handler for the blur event on focusable elements. Prefer addEventListener in apps.

✓ Baseline · Widely available

onblur

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support blur / onblur.

98% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer Legacy*
Supported*
blur Widely supported

Bottom line: *Safe on modern browsers. Remember: blur does not bubble — use focusout for delegation.

Key Takeaways

  • Role: run script when an element loses focus.
  • Opposite: focus / onfocus.
  • Delegation: use focusout (bubbles) or capture.
  • Not cancelable — focus will leave.

One line: onblur runs when focus leaves an element — prefer listeners, and use focusout when you need bubbling.

Frequently Asked Questions

It is an event handler for the blur event. That event fires when an element loses focus — for example when the user tabs or clicks to another control.
No. blur does not bubble. The related focusout event does bubble, which makes parent-level listening easier for event delegation.
onfocus / the focus event — it fires when an element receives focus.
Prefer element.addEventListener("blur", handler). MDN discourages inline HTML event handler attributes.
No. The blur event is not cancelable. You cannot force the element to keep focus by canceling blur.
Common uses: validate or format a field when the user leaves it, clear a highlight, or update UI after focus moves away.

Next: oncanplay

Learn when media data is ready enough to start playback.

oncanplay tutorial →

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.

8 people found this page helpful