HTML onblur Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

The onblur attribute is an inline event handler that runs JavaScript when the blur event fires. That happens when a focusable element loses focus—for example when the user tabs to the next field or clicks outside an input. It is widely used for form validation, showing hints, or updating UI after editing. Pair it with onfocus for highlight-and-validate flows. The blur event does not bubble; use focusout if you need delegation on a parent.

What You’ll Learn

01

Event handler

Inline JS.

02

blur event

Loses focus.

03

Form fields

input, textarea.

04

+ onfocus

Focus / blur pair.

05

addEventListener

Preferred way.

06

.onblur

Property API.

Purpose of onblur Attribute

The primary purpose of onblur is to react when the user finishes interacting with a field—without waiting for form submit. Common uses include validating email format, checking required fields, formatting phone numbers, or removing a focus highlight. Because blur fires when focus moves away, it feels natural for “check this when I’m done typing here” feedback.

Do not confuse onblur with onbeforeunload. Blur is for individual elements losing focus inside the page. beforeunload is for leaving the entire page. Also note that blur does not bubble, unlike focusout.

💡
Prefer addEventListener on the element

Production code should use input.addEventListener("blur", handler). Inline onblur on <input> works for tutorials but mixes markup and behavior.

📝 Syntax

Set onblur on a focusable element or assign element.onblur:

onblur.html
<script>

  function validateEmail() {

    const email = document.getElementById("email").value;

    console.log("Validate:", email);

  }

</script>



<label for="email">Email</label>

<input type="email" id="email" onblur="validateEmail()">

Syntax Rules

  • Value is JavaScript code executed when the element loses focus.
  • Works on focusable elements: input, textarea, select, button, a.
  • Pair with onfocus / focus for enter-and-leave field behavior.
  • JavaScript: element.onblur = function() { … }.
  • Modern alternative: element.addEventListener("blur", handler).
  • blur does not bubble; use focusout on a parent for delegation.

💎 Values

The onblur attribute accepts a string of JavaScript code:

  • onblur="validateEmail()" — Call a named function.
  • onblur="this.classList.remove('focused')" — Inline statement.
  • JavaScript: field.onblur = () => { … } assigns the handler.
onblur-js.html
const email = document.getElementById("email");

email.addEventListener("blur", () => {

  if (!email.value.includes("@")) {

    email.setAttribute("aria-invalid", "true");

  }

});

email.addEventListener("focus", () => {

  email.removeAttribute("aria-invalid");

});

⚡ Quick Reference

EventWhen it firesHandler
blurElement loses focusonblur
focusElement gains focusonfocus
changeValue changed + bluronchange
ValidationRun on blurCommon pattern
Bubbles?blur no; focusout yesDelegation tip

Applicable Elements

TargetSupported?Notes
<input>, <textarea>YesMost common — form validation
<select>, <button>YesFocusable controls
<a href>YesLink loses focus
<div> (no tabindex)NoNot focusable by default
windowNoUse onbeforeunload for page leave

onblur vs onfocus vs onchange

HandlerWhen it firesTypical use
onfocusField receives focusHighlight, show hint
onblurField loses focusValidate, remove highlight
onchangeValue changed and committedReact to new value only

Examples Gallery

Inline onblur email validation, addEventListener on an input, and pairing with onfocus.

👀 Live Preview

Enter an email, then tab away or click outside the field:

Blur the field to validate.

Example — Inline onblur on input

Validate when the user leaves the email field:

inline-onblur.html
<label for="email">Email</label>

<input type="email" id="email" onblur="validateEmail()">



<script>

  function validateEmail() {

    const value = document.getElementById("email").value;

    console.log("Validate:", value);

  }

</script>
Try It Yourself

How It Works

The browser fires blur on the input when it loses focus. The inline onblur attribute calls your validation function at that moment.

Dynamic Values with JavaScript

Attach the handler with addEventListener:

dynamic-onblur.html
<script>

  document.getElementById("name").addEventListener("blur", function () {

    console.log("Field lost focus:", this.value);

  });



  // Or property form:

  document.getElementById("name").onblur = function () { /* … */ };

</script>
Try It Yourself

How It Works

Register the listener once at page load. Each time the field loses focus, your handler runs with this pointing at the element.

Example — Pair with onfocus

Highlight on focus, validate on blur:

focus-blur-pair.html
field.addEventListener("focus", () => {

  field.classList.add("is-focused");

});



field.addEventListener("blur", () => {

  field.classList.remove("is-focused");

  validate(field);

});
Try It Yourself

How It Works

focus runs when the user enters the field; blur runs when they leave. Together they create responsive form UX.

♿ Accessibility

  • Announce errors on blur — Link validation messages with aria-describedby and set aria-invalid="true" when validation fails.
  • Do not rely on color alone — Pair error styling with visible text so screen reader users get feedback.
  • Avoid alert() on blur — Inline error text near the field is more accessible than modal alerts.
  • Keyboard navigation — Blur fires when tabbing away; ensure tab order is logical before validating.
  • Focus management — Do not trap focus or move it unexpectedly in blur handlers.

🧠 How onblur Works

1

User focuses field

Click or Tab into input.

focus
2

User leaves field

Tab away or click outside.

Trigger
3

blur event fires

Handler runs on element.

onblur
=

Validate or update UI

Show feedback.

Browser Support

The blur event and onblur handler are supported in all modern browsers — Chrome, Firefox, Safari, and Edge. Behavior is consistent for standard focusable form controls.

DOM Events · Fully supported

Universal onblur support

All major browsers fire the underlying event and honor the onblur handler attribute.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 9+ supported
Full support
Opera Fully supported
Full support
onblur attribute 99% supported

Bottom line: Universal support on focusable elements. Test validation UX with keyboard-only navigation.

💡 Best Practices

✅ Do

  • Use addEventListener on the element — Clearer than inline onblur.
  • Pair with onfocus — Highlight on enter, validate on leave.
  • Validate on blur, not every keystroke — Less noisy for long text fields; use input for live hints if needed.
  • Keep handlers fast — Avoid slow network calls blocking focus moves.
  • Use focusout for delegation — blur does not bubble to parent containers.

❌ Don’t

  • Prefer HTML5 validation — Use built-in required, pattern, and type where possible.

Conclusion

The onblur attribute runs JavaScript when a focusable element loses focus and the blur event fires. Use it for form validation, formatting, and removing focus styles after the user finishes editing a field.

Pair it with onfocus, prefer addEventListener("blur", …), and remember that blur does not bubble — use focusout for event delegation.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onblur

Bookmark these before wiring your next event handler.

5
Core concepts
🌐 02

Form fields

input, textarea.

Scope
🔄 03

Validate on leave

With onfocus.

Pattern
📝 04

addEventListener

Preferred.

Modern
05

No bubbling

Use focusout.

Note

❓ Frequently Asked Questions

It runs JavaScript when the blur event fires — when a focusable element loses focus.
input, textarea, select, button, and focusable links. Non-focusable elements need tabindex.
onblur fires every time focus leaves. onchange fires when the value changed and the field is committed (often on blur for text inputs).
No. Use the focusout event if you need delegation on a form or container.
Prefer element.addEventListener("blur", …). Inline onblur works for simple demos.
No. onblur is when a field loses focus inside the page. onbeforeunload is when the user tries to leave the entire page.

Validate form fields on blur

Practice inline onblur, addEventListener, and onfocus pairing in the Try It editor.

Try onblur demo →

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