Execute a script when the element gets focus in HTML?

HTMLWeb DevelopmentFront End Technology

Use the onfocus attribute to execute a script when the element gets focus in HTML.

Example

You can try to run the following code to implement the onfocus attribute −

<!DOCTYPE html>
<html>
   <body>
      <p>Enter subject name below,</p>
      Subject: <input type = "text" id = "sname" onfocus = "display(this.id)">
      <br>

      <script>
         function display(val) {
            document.getElementById(val).style.background = "blue";
         }
      </script>
   </body>
</html>
raja
Published on 14-Mar-2018 16:21:06
Advertisements