Horje
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

Browser Support


How to Execute a JavaScript when an input field gets focus

Input Example. A function is triggered when one of the input fields get focus. The function changes the background-color of the input field.
index.html
Example: HTML
First name: <input type="text" id="fname" onfocus="myFunction(this.id)"><br>
Last name: <input type="text" id="lname" onfocus="myFunction(this.id)">

<script>
function myFunction(x) {
  document.getElementById(x).style.background = "yellow";
}
</script>

Output should be:

How to Execute a JavaScript when an input field gets focus




html onfocus attribute

Type:
Develop
Category:
Web Tutorial
Sub Category:
HTML Attribute
Uploaded by:
Admin


Read Article
https://develop.horje.com/learn/1434/reference