Home >Web Front-end >HTML Tutorial >Execute script when HTML element gets focus?

Execute script when HTML element gets focus?

WBOY
WBOYforward
2023-08-29 18:21:021363browse

Execute script when HTML element gets focus?

Use the onfocus attribute to execute a script when focus is obtained in HTML.

Example

You can try running 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>

The above is the detailed content of Execute script when HTML element gets focus?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete