HTML onfocus Attribute

The onfocus attribute fires the moment that the element gets focus."

Definition and Usage

The onfocus attribute fires the moment that the element gets focus.

Onfocus is most often used with <input>, <select>, and <a>.

Tip: The onfocus attribute is the opposite of the onblur attribute.

Applies to

The onfocus attribute is part of the Event Attributes, and can be used on any HTML elements.

Elements Event
All HTML elements onfocus

Example

Input Example

Execute a JavaScript when an input field gets focus:

<input type="text" id="fname" onfocus="myFunction(this.id)">

Browser Support

Event Attribute
onfocus Yes Yes Yes Yes Yes