The onfocus EventThe onfocus event occurs when an object gets the focus (that is, becomes the target of keystrokes). You can find the support for this event in Table 6.69. Table 6.69. The onfocus Event
Here's an example that uses the onfocus event to display prompts to the user about what kind of data he should enter. This example is a web pagebased calculator that adds numbers , and each time one of the two text fields the user is supposed to enter an integer into gets the focus, this code uses the window.status property to display the prompt Enter an integer. in the browser's status bar: (Listing 06-16.html on the web site)<HTML> <HEAD> <TITLE>An HTML Calculator</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- function text1Prompt() { window.status = "Enter an integer." } function text2Prompt() { window.status = "Enter an integer." } function text3Prompt() { window.status = "The sum is displayed here." } function Add() { document.form1.text3.value = parseInt(document.form1.text1.value) + parseInt(document.form1.text2.value) } // --> </SCRIPT> </HEAD> <BODY> <H1>An HTML Calculator</H1> <FORM NAME="form1"> <INPUT TYPE="TEXT" NAME="text1" ONFOCUS="text1Prompt()"> + <INPUT TYPE="TEXT" NAME="text2" ONFOCUS="text2Prompt()"> <INPUT TYPE="BUTTON" ONCLICK="Add()" VALUE = " = "> <INPUT TYPE="TEXT" NAME="text3" ONFOCUS="text3Prompt()"> </FORM> </BODY> </HTML> You can see the results in Figure 6.12note the message Enter an integer. in the status bar. Figure 6.12. Using the onfocus event.
|