HTML onfocus Event Attribute HTML Reference



Example

Execute a JavaScript when an input field gets focus:

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

Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

1. 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.

Note: The onfocus attribute CANNOT be used with: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, or <title>.


2. Differences Between HTML 4.01 and HTML5

None.


3. Syntax

<element onfocus="script">

4. Attribute Values

ValueDescription
scriptThe script to be run on onfocus

Relative articles