Use the onfocus attribute to execute a script when focus is obtained in HTML.
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!