Click to return to the DHTML, HTML     
onfinish Event     onhelp Event     DHTML Events    
Web Workshop  |  DHTML, HTML & CSS

onfocus Event


Fires when the object receives the focus.

Syntax

Inline HTML<ELEMENT onfocus = "handler" ... >All platforms
Event propertyobject.onfocus = handlerJScript (compatible with ECMA 262 language specification) only
Named script<SCRIPT FOR = object EVENT = onfocus>Internet Explorer only

Remarks

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

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

As of Microsoft® 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.

Elements cannot receive focus until the document is finished loading.

As of 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.

Event Object Properties

Although event handlers in the document object model do not receive parameters directly, the handler can query the event object for data.

Event Object Properties

clientXRetrieves the x-coordinate of the mouse cursor relative to the client area of the window, excluding window decorations or scroll bars.
clientYRetrieves the y-coordinate of the mouse cursor relative to the client area of the window, excluding window decorations or scroll bars.
offsetXRetrieves the horizontal coordinate of the mouse's position relative to the object firing the event.
offsetYRetrieves the vertical coordinate of the mouse's position relative to the object firing the event.
returnValueSets or retrieves the return value from the event.
screenXRetrieves the horizontal position of the mouse, in pixels, relative to the user's screen.
screenYRetrieves the vertical position of the mouse, in pixels, relative to the user's screen.
srcElementRetrieves the object that fired the event.
typeRetrieves the event name from the event object.
xRetrieves the x-coordinate of the mouse cursor relative to the parent element.
yRetrieves the y-coordinate of the mouse cursor relative to the parent element.

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.

Sample Code

<STYLE>
.accessible {
   background-color: "#AAAAFF";
   font-weight: bold;
   font-size: 12pt;
}
</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 Internet Explorer 4.0 or later. Click the icon below to install the latest version. Then reload this page to view the sample.
Microsoft Internet Explorer

Applies To

[ Object Name ]
PlatformVersion
Win16:
Win32:
Mac:
Unix:
WinCE:
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


Back to topBack to top

Did you find this topic useful? Suggestions for other topics? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.