The onfocus
property of the GlobalEventHandlers
mixin is an EventHandler
that processes focus
events on the given element.
The onfocus property of the GlobalEventHandlers mixin is an EventHandler that processes focus events on the given element. The focus event is raised when the user sets focus on an element. For onfocus to fire on non-input elements, they must be given the tabindex attribute (see Building keyboard accessibility back in for more details). Being wholly-owned by the non-profit Mozilla Foundation means we can focus on the real needs of people, rather than a bottom line. The web the way it should be Join a global community of employees, collaborators, activists, and advocates fighting to keep the internet healthy for generations.

The focus
event is raised when the user sets focus on an element.

Mozilla Focus Css
For onfocus
to fire on non-input elements, they must be given the tabindex
attribute (see Building keyboard accessibility back in for more details).
Note: The opposite of onfocus
is onblur
.
Syntax
Value
functionRef
is a function name or a function expression. The function receives a FocusEvent
object as its sole argument.
Mozilla Focus For Mac
Example
This example uses onblur
and onfocus
to change the text within an <input>
element.
Mozilla Focus Github
HTML
JavaScript
Result
Try clicking in and out of the form field, and watch its contents change accordingly.
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'onfocus' in that specification. | Living Standard |

Browser compatibility


BCD tables only load in the browser
In contrast to IE, in which almost all kinds of elements receive the focus
event, almost all kinds of elements on Gecko browsers do NOT work with this event.
See also
focus
event- Related event handler:
GlobalEventHandlers.onblur
