ใช้แอตทริบิวต์ onfocus เพื่อรันสคริปต์เมื่อองค์ประกอบได้รับโฟกัสใน HTML
ตัวอย่าง
ลองรันโค้ดด้านล่างเพื่อใช้งานแอตทริบิวต์ onfocus:
<!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>