Fires when the object receives the focus.
Syntax
Inline HTML <ELEMENT onfocus = "handler" ... > All platforms Event property object.onfocus = handler JScript (compatible with ECMA 262 language specification) only Named script <SCRIPT FOR = object EVENT = onfocus> Internet Explorer only
Remarks
Bubbles No Cancels No 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.
clientX Retrieves the x-coordinate of the mouse cursor relative to the client area of the window, excluding window decorations or scroll bars. clientY Retrieves the y-coordinate of the mouse cursor relative to the client area of the window, excluding window decorations or scroll bars. offsetX Retrieves the horizontal coordinate of the mouse's position relative to the object firing the event. offsetY Retrieves the vertical coordinate of the mouse's position relative to the object firing the event. returnValue Sets or retrieves the return value from the event. screenX Retrieves the horizontal position of the mouse, in pixels, relative to the user's screen. screenY Retrieves the vertical position of the mouse, in pixels, relative to the user's screen. srcElement Retrieves the object that fired the event. type Retrieves the event name from the event object. x Retrieves the x-coordinate of the mouse cursor relative to the parent element. y Retrieves 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" >
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