HTML onfocus Attribute

❮ HTML Attributes

Example

First name: <input type="text" id="fname" onfocus="myFunction(this.id)"><br>
Last name: <input type="text" id="lname" onfocus="this.style.background='yellow'">

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

Meaning

The onfocus attribute specifies that an element has received focus; namely, it has been selected for manipulation or data entry.




Standard Syntax

<element onfocus="script">



Browser Support




Status




Applies to:

The onfocus attribute can be used on the following element:

Element Attribute
All visible elements. onfocus






Attribute Values

Value Description
script Specifies the script to be run on onfocus
❮ HTML Attributes