onblur event | blur event
| A A | Font size |
|
|
Share |
|
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.
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.
- 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.
How to register:
In HTML:
In JavaScript:
| <ELEMENT onblur="handler"> |
In JavaScript:
| object.onblur = handler; | |||||
| object.attachEvent ("onblur", handler); | |||||
| object.addEventListener ("blur", handler, useCapture); |
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 |
| Type | 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. |
|
||||||||
| Any other action that invokes the onblur event. |
|
Example HTML code 1:
This example illustrates the use of the onblur event:
|
|
||||
<head> <script> 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.<br /> <input type="text" onfocus="OnFocusInput (this)" onblur="OnBlurInput (this)" value="Custom input field"/> </body> |
||||
|
||||
|
Did you find this example helpful?
|
Supported by objects:
document, window
HTML elements:
a, abbr, acronym, address, applet, area, b, bdo, big, blockquote, body, button, caption, center, cite, code, dd, del, dfn, dir, div, dl, dt, em, embed, fieldset, font, form, frame, frameset, h1, h2, h3, h4, h5, h6, hr, html, i, iframe, img, input:button, input:checkbox, input:file, input:image, input:password, input:radio, input:range, input:reset, input:search, input:submit, input:text, ins, isindex, kbd, keygen, label, legend, li, listing, map, marquee, menu, nobr, noframes, object, ol, p, plaintext, pre, q, rt, ruby, s, samp, select, small, span, strike, strong, sub, sup, table, tbody, td, textarea, tfoot, th, thead, tr, tt, u, ul, var, xmp
Related pages:
External links:
User Contributed Comments

