HTML onfocusEvent Attributes
Examples
Run a script when an input field gets focus:
<input type="text" id="fname" onfocus="setStyle(this.id)">
Try it Yourself »
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the onfocus event attribute.
Definition and Usage
The onfocus attribute triggers when an element gets focus.
onfocus is commonly used in <input>, <select>, and <a> elements.
Tip:The event attribute opposite to the onfocus attribute isonblur 。
Note:The onfocus attribute cannot be used on the following elements: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, or <title>.
Differences between HTML 4.01 and HTML5
None.
Syntax
<element onfocus="script">
Attribute Values
| Value | Description |
|---|---|
| script | Specifies the script to be executed when the onfocus event triggers. |
HTML Event Attributes Other Extensions