You are here: Reference > JavaScript > client-side > event handling > events > onblur

onblur event | blur event

Browser support:
Occurs when an element loses focus.
An element with focus is always the active element in a document, but an active element does not necessarily have focus. For example, an active element within a window that is not the foreground window has no focus. Only one element can be active at a time in a document.
  • You can get the active element with the activeElement property.
  • To set the active element in a document, use the focus and setActive methods.
  • To determine whether the active element has focus, use the hasFocus method.
The onblur event is not cancelable. If you want to prevent the deactivation of an element, cancel the onbeforedeactivate event. For a detailed description, see the page for onbeforedeactivate event.
To detect when an element receives focus, use the onfocusin, DOMFocusIn and onfocus events.

How to register:

In HTML:
<ELEMENT onblur="handler">

In JavaScript:
object.onblur = handler;
object.addEventListener ("blur", handler, useCapture);
9
object.attachEvent ("onblur", handler);
You can find the related objects in the Supported by objects section below.
The event object is accessible to all event handlers in all browsers. The properties of the event object contain additional information about the current event. To get further details about these properties and the possible event handler registration methods, please see the page for the event object.
For a complete list of events, see the page for Events in JavaScript.

Basic information:

Bubbles No
Cancelable No
Event object
FocusEvent
9
Event

Actions that invoke the onblur event:

  • Clicking outside the active element.
  • Navigating away from the active element with the TAB or an access key.
  • Switching to another document or application.
  • Invoking the setActive method on a non-active element that can be active.
  • Invoking the focus method on a non-active element that can be active.
  • Invoking the blur method on the active element.

The order of events related to the onblur event:

Action Event order
Switching to another document or application.
Internet Explorer Firefox Opera Google Chrome and Safari
  1. onfocusout
  2. onblur
  1. onblur
Any other action that invokes the onblur event.
Internet Explorer Firefox Opera Google Chrome and Safari
  1. onbeforedeactivate
  2. ondeactivate
  3. onfocusout
  4. onblur
  1. onblur
  1. onblur
  2. DOMFocusOut
  1. onblur
  2. DOMFocusOut

Example HTML code 1:

This example illustrates the use of the onblur event:
<head>
    <script type="text/javascript">
        function OnFocusInput (input) {
            input.style.color = "red";
        }

        function OnBlurInput (input) {
            input.style.color = "";
        }
    </script>
</head>
<body>
    When the following input field has the focus its text color will be red.
    Click into the input field!
    <br /><br />
    <input type="text" onfocus="OnFocusInput (this)" onblur="OnBlurInput (this)" value="Custom input field"/>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content