onfocus event | focus event
Occurs when an element receives 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 onfocus event is not cancelable.
If you want to prevent an element from getting the focus, set the disabled property of the element to true.
See the examples for details.
- 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.
To detect when an element loses focus, use the onblur event.
How to register:
In HTML:
In JavaScript:
<ELEMENT onfocus="handler"> |
In JavaScript:
object.onfocus = handler; | |||||||||||
object.addEventListener ("focus", handler, useCapture); |
| ||||||||||
object.attachEvent ("onfocus", 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 |
|
Actions that invoke the onfocus event:
- Clicking on a non-active element that can be active (see the setActive method).
- Navigating to an element with the TAB or an access key.
- Occurs on the active element when the document window comes to the foreground.
- 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.
The order of events related to the onfocus event:
Action | Event order | ||||||||
---|---|---|---|---|---|---|---|---|---|
If an element is the active element and the document window comes to the foreground. |
|
||||||||
Any other action that invokes the onfocus event. |
|
Example HTML code 1:
This example illustrates the use of the onfocus 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?
|
Example HTML code 2:
This example shows how to prevent an element from getting the focus:
|
||||
<head> <script type="text/javascript"> function OnAllowFocusChanged (checkbox) { var textField = document.getElementById ("myText"); textField.disabled = !checkbox.checked; } </script> </head> <body> <input type="checkbox" checked="checked" onchange="OnAllowFocusChanged (this)"/>Allow the text field to get the focus <br /> <input type="text" id="myText" value="Text 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