<FORM>
Enter the radius of a circle: <INPUT
TYPE="text"
NAME="number"
VALUE="25">
<INPUT TYPE="button"
VALUE="Find the circumference"
onClick="form.result1.value
= 2 * Math.PI * form.number.value">
<INPUT
TYPE="text"
NAME="result1">
<INPUT
TYPE="button"
VALUE="Find the square root of the
first number"
onClick="form.result2.value
= Math.sqrt(form.number.value)">
<INPUT
TYPE="text"
NAME="result2">
</FORM>
NOTES:
- A form element named abc can be accessed through
JavaScript as form.abc (if the form is given a name, say
"stuff" in the <FORM>
tag, this name can also be used to access a form element, e.g.,
stuff.abc.
- The value of some elements, for example of text input elements,
can not only be accessed but also changed via form.abc.value.
- <INPUT
TYPE="button"
VALUE="Find the circumference"
onClick="form.result1.value
= 2 * Math.PI * form.number.value">
adds a button form element to the form. When the button is
clicked JavaScript takes the value in the form element named "number"
(i.e., form.number.value) and multiples
that number by 2 and Math.PI - giving
us the circumference of a circle. This is then assigned to form.result1.value,
setting the value of the "result1"
element (input box) in the form.
- When the 2nd button is clicked
JavaScript takes the value in the form element named "number"
(i.e., form.number.value) and passes
that number to Math.sqrt. The value
returned is just the square root of the argument. This is then
assigned to form.result2.value, setting
the value of the "result2"
element (input box) in the form.
When a number may be entered into the form's "number"
element and one of the buttons is pressed, the number's square root or
circumference is calculated, depending on which button is pressed. The
results of the calculation are displayed in the form's "result1"
element or the form's "result2"
element depending on which button is clicked. |