Passing a form by reference
The effect
In this page, we're passing a pointer to an object ( this.form ) to our functions.
To make it clear that we're not seeing a global value, the parameter is received in the function under a
different name, paramform. However, since the object is passed by reference,
changes made in this "parameter form" by the functions are reflected in the actual form in the page.
Although objects are passed by reference, parameters of simple datatypes would be passed by value. Any
changes made to these parameters in the function would not appear in the original variables.
(Code adapted from the Official Netscape JavaScript Book by Peter Kent and John Kent,
Netscape Press, 1996.)
Function definitions in the page header
<script language="JavaScript" type="text/javascript">
<!--
function SolveEquation(paramform) {
paramform.txtResult.value = eval(paramform.txtCalc.value) ;
}
function ClearForm(paramform) {
paramform.txtResult.value = "" ;
paramform.txtCalc.value = "" ;
}
//-->
</script>
The form
<FORM>
<table summary="" cellpadding=5 cellspacing=0 border=2>
<tr><td align=right>
<b>Enter a calculation to solve</b><br>
Example: ( 3 * 7 ) / 11
</td><td align=left>
<INPUT TYPE="text" NAME="txtCalc" SIZE=20>
</td></tr><tr><td align=right>
<INPUT TYPE="button" VALUE="Clear" onclick="ClearForm(this.form)">
<INPUT TYPE="button" VALUE="Evaluate!" onclick="SolveEquation(this.form)">
</td><td align=left>
<INPUT TYPE="text" NAME="txtResult" SIZE=20>
</td></tr></table>
</FORM>