Skip to content
Related Articles
Open in App
Not now

Related Articles

HTML | onfocus Event Attribute

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 16 Aug, 2022
Improve Article
Save Article

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.
Supported Tags: It supports all HTML elements which are given below: 

  • <base> 
  •  <bdo>
  •  <br>
  •  <head>
  •  <html>
  •  <iframe>
  •  <meta>
  •  <param>
  •  <script>
  •  <style>
  •   <title>


<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 3.1
  • Google Chrome 1
  • Edge 12
  • Firefox 24
  • Opera 11.6
  • Internet Explorer 9


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!