HTML onfocus Attribute

❮ HTML Attributes


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

function myFunction(x) {
  document.getElementById(x).style.background = "yellow";


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


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