Last Updated : 17 Sep, 2024
This onfocus attribute works when the element gets focused.
This event attribute is mostly used with <input>, <select>, <a> elements.
Supported Tags:It supports all HTML elements which are given below:
<element onfocus = "script">Attribute Value:
The script value run when onfocus attribute called.
Example:
html
<!DOCTYPE html>
<html>
<head>
<title>onfocus attribute</title>
<style>
h1 {
color: green;
}
body {
text-align: center;
}
input {
color: white;
margin-top: 5px;
border: 1px solid black;
border-radius: 3px;
}
</style>
<script>
function sudo(x) {
document.getElementById(x).style.background = "green";
}
</script>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>onfocus event attribute</h2>
First name:
<input type="text" id="fname"
onfocus="sudo(this.id)"><br>
Last name:
<input type="text" id="lname"
onfocus="sudo(this.id)">
</body>
</html>
Output:
Supported Browsers:The browsers supported by onfocus attribute are listed below:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4