Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

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

Example

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