onfocus Event

Internet Development Index

Fires when the object receives focus.

Syntax

Inline HTML<ELEMENT onfocus = "handler" ... > All platforms
Event propertyobject.onfocus = handlerJScript only
object.onfocus = GetRef("handler")Visual Basic Scripting Edition (VBScript) 5.0 or later only
Named script <SCRIPT FOR = object EVENT = onfocus> Internet Explorer only

Event Information

BubblesNo
CancelsNo
To invoke
  • Click an object.
  • Use keyboard navigation.
  • Invoke the focus method.
  • Invoke the setActive method.
Default action Sets focus to an object.

Event Object Properties

Although event handlers in the DHTML Object Model do not receive parameters directly, a handler can query an event object for data.

Available Properties

altKey Sets or retrieves a value that indicates the state of the ALT key.
altLeft Sets or retrieves a value that indicates the state of the left ALT key.
clientX Sets or retrieves the x-coordinate of the mouse pointer's position relative to the client area of the window, excluding window decorations and scroll bars.
clientY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the client area of the window, excluding window decorations and scroll bars.
ctrlLeft Sets or retrieves the state of the left CTRL key.
offsetX Sets or retrieves the x-coordinate of the mouse pointer's position relative to the object firing the event.
offsetY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the object firing the event.
returnValue Sets or retrieves the return value from the event.
screenX Retrieves the x-coordinate of the mouse pointer's position relative to the user's screen.
screenY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the user's screen.
shiftLeft Retrieves the state of the left SHIFT key.
srcElement Sets or retrieves the object that fired the event.
type Sets or retrieves the event name from the event object.
x Sets or retrieves the x-coordinate, in pixels, of the mouse pointer's position relative to a relatively positioned parent element.
y Sets or retrieves the y-coordinate, in pixels, of the mouse pointer's position relative to a relatively positioned parent element.

Remarks

Note  Using the setActive method has no effect on document focus. Using the focus method on an individual element causes the element to gain focus and become the active element.

When one object loses activation and another object becomes the activeElement, the onfocus event fires on the object becoming the activeElement only after the onblur event fires on the object losing activation. Use the focus events to determine when to prepare an object to receive input from the user.

Elements cannot receive focus until the document is finished loading.

As of Microsoft® Internet Explorer 5, elements retain focus within the current browser history when the user returns to a page. To avoid firing the onfocus event unintentionally for an element when the document loads, invoke the focus method on another element.

As of Internet Explorer 5, you can force elements that do not implicitly receive focus to receive focus by adding them to the document tabbing order using the TABINDEX attribute.

For Internet Explorer 5.5 and later, focus on a document, and the active element of a document can be managed separately. The synchronous events onactivate and ondeactivate provide better control for managing activation changes.

Example

This example uses the onfocus event to make INPUT_text and label objects more accessible. When the INPUT_text object has focus, the onfocus event fires and the backgroundColor, fontSize, and fontWeight properties are changed to give the control more prominence.

...
<STYLE>
.normal {background-color:"#FFFFFF"; color:"#000000"; font-weight:normal;
font-size:8pt; font-family:Arial;}
.accessible { background-color:"beige"; font-weight:bold; font-size:10pt;}
</STYLE>
<SCRIPT>
function fnSetStyle(){
event.srcElement.className="accessible";
var oWorkLabel=eval(event.srcElement.id + "_label");
oWorkLabel.className="accessible";
}
</SCRIPT>
<LABEL FOR="oInput" CLASS="normal" ID="oInput_label">Enter some text</LABEL>
<INPUT TYPE="text" CLASS="normal" onfocus="fnSetStyle()" ID="oInput">
...
This feature requires Microsoft® Internet Explorer 4.0 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

Standards Information

This event is defined in HTML 4.0 Non-Microsoft link.

Applies To

A, ACRONYM, ADDRESS, APPLET, AREA, B, BDO, BIG, BLOCKQUOTE, BUTTON, CAPTION, CENTER, CITE, CUSTOM, DD, DEL, DFN, DIR, DIV, DL, DT, EM, EMBED, FIELDSET, FONT, FORM, FRAME, FRAMESET, hn, HR, I, IFRAME, IMG, INPUT type=button, INPUT type=checkbox, INPUT type=file, INPUT type=hidden, INPUT type=image, INPUT type=password, INPUT type=radio, INPUT type=reset, INPUT type=submit, INPUT type=text, INS, ISINDEX, KBD, LABEL, LEGEND, LI, LISTING, MARQUEE, MENU, 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, window, XMP

See Also

blur, focus, onfocusin