Skip to content
Related Articles

Related Articles

HTML | onfocus Event Attribute
  • Difficulty Level : Basic
  • Last Updated : 03 Dec, 2018

This onfocus attribute works when the element gets focused. 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.


<element onfocus = "script">

Attribute Value: The script value run when onfocus attribute called.


<!DOCTYPE html>
        <title>onfocus attribute</title>
            h1 {
            body {
            input {
                border:1px solid black;
            function sudo(x) {
                document.getElementById(x).style.background = "green";
        <h2>onfocus event attribute</h2>
        First name: <input type="text" id="fname" 
        Last name: <input type="text" id="lname"


Supported Browsers: The browser supported by onfocus attribute are listed below:

  • Apple Safari
  • Google Chrome
  • Firefox
  • Opera
  • Internet Explorer

My Personal Notes arrow_drop_up
Recommended Articles
Page :