asp.net.ph

onfocus Event

DHTML Events


Fires when the object receives the focus.

Syntax


Inline HTML <element onfocus = "handler" ... > All platforms
Event property object.onfocus = handler ECMA-262 Language Specification
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.

A focus event occurs when an element receives focus either via a pointing device or by tabbing navigation. This event is valid for the following elements: LABEL, INPUT, SELECT, TEXTAREA, and BUTTON, as well as the window object.

In addition, Internet Explorer® supports the event in any element that can receive the focus, such as links in anchors and area elements.

When focus is switched from one object to another, the onfocus event fires on the object receiving focus only after the onblur event fires on the object losing focus. The focus events are useful for determining when to prepare an object to receive input from the user.

Event Object Properties

While event handlers in the Document Object Model do not receive parameters directly, the handler can query the event object for data.

Event Object Properties


Example

The following sample demonstrates the use of the onfocus event to make an INPUT Text and LABEL object 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.

<head>
. . .
<style type="text/css">
<!--
 .input {background-color:ivory;color:navy}
 .label {color:maroon;font:bold}
—>
</style>

<script language="JavaScript">
<!--
function setStyle ( ) {
event.srcElement.className="input";
var labelInFocus=eval(event.srcElement.id + "_label");
labelInFocus.className="label"
}
function defStyle ( ) {
event.srcElement.className="";
var labelInFocus=eval(event.srcElement.id + "_label");
labelInFocus.className=""
}
//—>
</script>
</head>

<body>
. . .
<table align="center" cellpadding=5>
<col align="right">
<tr>
  <td><label for="txt1" id="txt1_label">
    Enter some text</label></td>
  <td><input type="text" onfocus="setStyle()"
    onblur="defStyle()" id="txt1"></td></tr>
<tr>
  <td><label for="txt2" id="txt2_label">
    Enter more text</label></td>
  <td><input type="text" onfocus="setStyle()"
    onblur="defStyle()" id="txt2"></td></tr>
</table>
</body>

 Show me 

Applies To

BUTTON, INPUT, LABEL, SELECT, TEXTAREA, window

See Also

blur, focus, onblur, onchange, onclick




Contents
HTML Elements
HTML Element Attributes
CSS Attributes
DOM Objects
DOM Object Properties
DOM Events Reference
DOM Methods Reference
DOM Collections Reference
 

Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph