asp.net.ph

Skip Navigation Links

onfocus Event


Fires when the object receives the focus.

Syntax

Inline <element onfocus = "handler" ... >
Script object.onfocus = handler

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, most browsers support 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 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.

Sample Code

 Show me 

Applies To

BUTTON, INPUT, LABEL, SELECT, TEXTAREA, window

See Also

blur, focus, onblur, onchange, onclick


Need a break ?
Suggested Reading

Check out related books at Amazon

© 2025 Reynald Nuñez and asp.net.ph.

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