Home HTML Attributes onblur HTML onblur Attribute Definition
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.
Syntax
HTML Syntax script runs when the element loses focus. Works on focusable elements (inputs, buttons, links with href, etc.). Preferred: element.addEventListener("blur", handler).
Values
Attribute Values How you listen for blur:
Value / form Description Try it JavaScript on element Inline handler, e.g. checkEmail() Try itaddEventListener("blur")Preferred modern listener Try itfocusout on parentBubbling alternative for delegation Try it
Try it
Live Preview Focus the field, then Tab away or click elsewhere. Status updates when blur fires.
Code
HTML Examples Short study snippets. Use Try It Yourself for a live editor. Prefer listeners in real apps.
1. Inline onblur 2. addEventListener 3. focusout on parent Example 1 — Inline onblur on an input Recognize the attribute form. Leaving the field updates status.
<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> How It Works onblur registers an inline handler on the input.Focus the field (click or Tab).Leave with Tab or another click — blur fires.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.
<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> How It Works No attribute — the listener is attached in script.On blur the handler trims leading/trailing spaces.Status confirms the cleaned value.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.
<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> Leave any field inside the form.
How It Works One listener on the form watches all nested inputs.focusout bubbles , so the parent receives the event.event.target is the field that lost focus.Alternative: listen for blur with capture (true) on the parent. Edge Cases & Pitfalls Most confusion comes from bubbling and cancelability.
bubble blur does not bubbleA 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.
Compatibility
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.
Remember
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 What does the HTML onblur attribute do? 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.
Does the blur event bubble? No. blur does not bubble. The related focusout event does bubble, which makes parent-level listening easier for event delegation.
What is the opposite of onblur? onfocus / the focus event — it fires when an element receives focus.
Should I use the attribute or addEventListener? Prefer element.addEventListener("blur", handler). MDN discourages inline HTML event handler attributes.
Can I cancel blur? No. The blur event is not cancelable. You cannot force the element to keep focus by canceling blur.
When is onblur useful? Common uses: validate or format a field when the user leaves it, clear a highlight, or update UI after focus moves away.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion