Add event listener to input text focus on event in JavaScript
Description
The following code shows how to add event listener to input text focus on event.
Example
<html>
<head>
<script language="JavaScript" type = "text/javascript">
function DisplayMsg(NumVal) {<!--from w ww .jav a2s . co m-->
if (NumVal == 1) {
alert("Type your name in the field");
}
if (NumVal == 2) {
alert("Type your phone number in the field");
}
}
</script>
<title>Keyboard Event</title>
</head>
<body>
<form name="form1">
<b>Name:</b> <input type = "text" name = "text1" onFocus="DisplayMsg(1)" size="20"><P>
<b>Phone:</b> <input type = "text" name = "text2" onFocus="DisplayMsg(2)" size="20"></p>
</form>
</body>
</html>
The code above generates the following result.
Javascript Tutorial Focus Event
Add event handler for input text element on...
Add event handler for input text element on...