HTML onfocus attribute
onfocus
The purpose of the HTML onfocus attrwibute is to indicate the user agent that the element has got focus. When the element gets focus, if onfocus is used, a script is executed.
Supported elements
HTML onfocus attribute supports a, area, button, input, label, select, textarea elements.
onfocus attribute can be effectively applied to form elements.
Syntax
<ElementName onfocus="value" >.....</ElementName>
Where ElementName is any supported element.
Type of value
Script.
Value
A script which is to be executed.
Default value
There is no default value of HTML onfocus attribute.
Supported doctypes
HTML 4.01 strict, HTML 4.01 transitional, HTML 4.01 frameset.
Example of HTML onfocus attribute with form elements
Result

View this example in a separate browser window
Example of HTML onfocus attribute with form elements
Previous: onkeypress, onkeydown, onkeyup event attributes of HTML elements
Next: HTML onload attribute
Test your Programming skills with w3resource's quiz.