HTML onfocus Attribute

HTML onfocus Attribute: This attribute is defined as it fires at the moment when the element is focused. And this event attribute is mostly used with <input>, <select>, <a> elements. This event attribute is supported by all HTML elements excepts <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title> elements.

HTML onfocus Attribute

This attribute can be applied to all the HTML elements.

Syntax: <element onfocus = “script”>

Browser Support

This attribute is supported by the following browsers:

  • Chrome
  • Internet Explorer
  • Safari
  • Opera
  • Firefox

Example: for <input> element

<!DOCTYPE html>
First name: <input type="text" id="fname" onfocus="myFunction(">
Last name: <input type="text" id="lname" onfocus="myFunction(">
function myFunction(x) {
  document.getElementById(x).style.background = "orange";


HTML onfocus attribute