Easy Tutorial
❮ Prop Ol Reversed Jsref Tutorial ❯

onfocus Event

Event Object

Example

Execute a JavaScript code snippet when an input field gains focus:


Definition and Usage

The onfocus event occurs when an object gains focus.

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

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

Syntax

In HTML:

In JavaScript:

In JavaScript, using the addEventListener() method:

Parameter Description
SomeJavaScriptCode Required. Specifies the JavaScript to be executed when the event occurs.

Browser Support

All major browsers support the onfocus event.


HTML Tags That Support the onfocus Event:

The onfocus attribute is typically used with all HTML elements, except: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title>.

❮ Prop Ol Reversed Jsref Tutorial ❯