Previous Example-|-Next Example-|-Return to Chapter Listing

Example 12.3:
onfocus: Form Modification


This uses the onfocus event handler. Enter numbers in the first two boxes, then Tab to (or click in) the last box. The onfocus handler adds the numbers.
Type number 1:
Type number 2:
Click in this box to see the total:



These are the scripts we used. First, in the HEAD: <SCRIPT LANGUAGE="JavaScript"> <!-- function runTotal(form) { var nNum1 = parseFloat(form.num1.value) var nNum2 = parseFloat(form.num2.value) form.total.value = nNum1 + nNum2 } //--> //--> </SCRIPT> Then we created this form element: <FORM> Type number 1: <INPUT TYPE="text" NAME="num1" VALUE="0"><BR> Type number 2: <INPUT TYPE="text" NAME="num2" VALUE="0"><BR> Click in this box to see the total: <INPUT TYPE="text" NAME="total" onfocus="runTotal(this.form)"> </FORM>
Previous Example-|-Next Example-|-Return to Chapter Listing