Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

onblur — handling loss of focus

The onblur attribute contains JavaScript that runs for the blur event when an element loses focus. It lets you respond to leaving a field regardless of whether the user changed its value.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

onblur="JavaScript statements"

onblur is an event handler content attribute for blur. Its value is JavaScript code, not a function name without a call. On an ordinary form field, it runs after focus is lost, for example when Tab moves focus to the next control.

Losing focus does not mean the value changed: leaving an unchanged field can also trigger onblur. Adding the attribute does not itself make an element focusable. On body and frameset, this attribute has special behavior: it handles the event on the Window object.

The HTML attribute contains program text; its corresponding JavaScript property, element.onblur, accepts a function or null. Attribute code requires JavaScript to be enabled and a security policy that permits such inline handlers.

In the example, click the field and press Tab. The status text changes from waiting to blur, even if you enter nothing.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>onblur example</title>
<label for="field">Text</label>
<input id="field" onblur="document.getElementById('result').textContent = 'blur'">
<button type="button">Next</button>
<p id="result" role="status">waiting</p>
</html>

Result

blur

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources