Fires when the object receives focus.
![]() |
Syntax
| HTML Attribute | <element onfocus = "handler(event)"> |
|---|---|
| Event Property | object.onfocus = handler; |
| addEventListener Method | object.addEventListener("focus", handler, useCapture) |
Event information
| Synchronous | No |
|---|---|
| Bubbles | No |
| Cancelable | No |
Event handler parameters
- pEvtObj [in]
-
Type: IHTMLEventObj
Standards information
- HTML 4.01 Specification, Section 18.2.3
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.
Sets focus to an object.
To invoke this event, do one of the following:
Examples
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 type="text/css">
.normal {
background-color: white;
color: black;
font-weight: normal;
font-size: 8pt;
font-family: Arial;
}
.accessible {
background-color: silver;
font-weight: bold;
font-size: 10pt;
}
</style>
<script type="text/javascript">
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">
...
See also
- a
- abbr
- address
- area
- audio
- b
- bdo
- blockQuote
- button
- canvas
- caption
- cite
- custom
- dd
- del
- div
- dl
- document
- dt
- em
- embed
- fieldSet
- form
- 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
- kbd
- label
- legend
- li
- media
- object
- ol
- p
- pre
- q
- rt
- ruby
- s
- samp
- select
- small
- source
- span
- strong
- sub
- sup
- table
- tBody
- td
- textArea
- tFoot
- th
- tHead
- tr
- u
- ul
- var
- video
- window
- Reference
- blur
- focus
- onfocusin
Send comments about this topic to Microsoft
Build date: 11/29/2012
