Tutorials References Exercises Bootcamp Menu
Sign Up Create Website Get Certified Upgrade

HTML onfocus Attribute

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


Input Example

Execute a JavaScript when an input field gets focus:

<input type="text" id="fname" onfocus="myFunction(this.id)">
Try it Yourself »

Browser Support

Event Attribute
onfocus Yes Yes Yes Yes Yes