👀 Live Preview
Enter an email, then tab away or click outside the field:
Blur the field to validate.

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.
Inline JS.
Loses focus.
input, textarea.
Focus / blur pair.
Preferred way.
Property API.
onblur AttributeThe 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.
Production code should use input.addEventListener("blur", handler). Inline onblur on <input> works for tutorials but mixes markup and behavior.
Set onblur on a focusable element or assign element.onblur:
<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()">input, textarea, select, button, a.onfocus / focus for enter-and-leave field behavior.element.onblur = function() { … }.element.addEventListener("blur", handler).blur does not bubble; use focusout on a parent for delegation.The onblur attribute accepts a string of JavaScript code:
onblur="validateEmail()" — Call a named function.onblur="this.classList.remove('focused')" — Inline statement.field.onblur = () => { … } assigns the handler.const email = document.getElementById("email");
email.addEventListener("blur", () => {
if (!email.value.includes("@")) {
email.setAttribute("aria-invalid", "true");
}
});
email.addEventListener("focus", () => {
email.removeAttribute("aria-invalid");
});| Event | When it fires | Handler |
|---|---|---|
blur | Element loses focus | onblur |
focus | Element gains focus | onfocus |
change | Value changed + blur | onchange |
| Validation | Run on blur | Common pattern |
| Bubbles? | blur no; focusout yes | Delegation tip |
| Target | Supported? | Notes |
|---|---|---|
<input>, <textarea> | Yes | Most common — form validation |
<select>, <button> | Yes | Focusable controls |
<a href> | Yes | Link loses focus |
<div> (no tabindex) | No | Not focusable by default |
window | No | Use onbeforeunload for page leave |
onblur vs onfocus vs onchange| Handler | When it fires | Typical use |
|---|---|---|
onfocus | Field receives focus | Highlight, show hint |
onblur | Field loses focus | Validate, remove highlight |
onchange | Value changed and committed | React to new value only |
Inline onblur email validation, addEventListener on an input, and pairing with onfocus.
Enter an email, then tab away or click outside the field:
Blur the field to validate.
Validate when the user leaves the email field:
<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>The browser fires blur on the input when it loses focus. The inline onblur attribute calls your validation function at that moment.
Attach the handler with addEventListener:
<script>
document.getElementById("name").addEventListener("blur", function () {
console.log("Field lost focus:", this.value);
});
// Or property form:
document.getElementById("name").onblur = function () { /* … */ };
</script>Register the listener once at page load. Each time the field loses focus, your handler runs with this pointing at the element.
Highlight on focus, validate on blur:
field.addEventListener("focus", () => {
field.classList.add("is-focused");
});
field.addEventListener("blur", () => {
field.classList.remove("is-focused");
validate(field);
});focus runs when the user enters the field; blur runs when they leave. Together they create responsive form UX.
aria-describedby and set aria-invalid="true" when validation fails.Click or Tab into input.
Tab away or click outside.
Handler runs on element.
Show feedback.
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.
onblur supportAll major browsers fire the underlying event and honor the onblur handler attribute.
Bottom line: Universal support on focusable elements. Test validation UX with keyboard-only navigation.
onblur.input for live hints if needed.blur does not bubble to parent containers.required, pattern, and type where possible.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.
onblurBookmark these before wiring your next event handler.
blur fires.
Eventinput, textarea.
ScopeWith onfocus.
PatternPreferred.
ModernUse focusout.
Noteblur 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).focusout event if you need delegation on a form or container.element.addEventListener("blur", …). Inline onblur works for simple demos.onblur is when a field loses focus inside the page. onbeforeunload is when the user tries to leave the entire page.Practice inline onblur, addEventListener, and onfocus pairing in the Try It editor.
5 people found this page helpful