The onfocus Event


The onfocus Event

The 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

Event

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

onfocus

x

x

x

x

x

x

x

x

x

x

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.

graphics/06fig12.gif



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net