PDA

View Full Version : Passing a form name to a function


spgrabel
07-02-2002, 07:27 PM
I can't seem to get this to work without hard coding the form name. I will be processing multiple forms on the page and I need to be able to change the amount field on a specific form on the page. Any other suggestions?


<html>
<head>
<title>Small Robe Orders</title>

<script LANGUAGE="JavaScript1.1">
function ChangeAmount(theForm){

document.theForm.amount.value="12.00";

}


</script>
</head>
<body>
<table width="600" height="400" bgcolor="#F8bf24" cellspacing="2" cellpadding="2" bordercolor="#0000ff" border="2" >
<th align="center"colspan="2">Kia's Kaps </th>
<tr><!-- Row 1 -->
<td align="center"><img src="../PinkPatriot.jpg" width="150" height="142" alt="" border="0">
<form name="form1" target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">

<input type="text" name="amount" value="0.00">
<input type="hidden" name="cn" value="Comments or Questions?">
<table>
<tr>
<td><input type="hidden" name="on0" value="Size">Size</td>

<td><select name="os0" onChange=ChangeAmount(this.form)>

<option value="Small">Small
<option value="Medium">Medium
<option value="Large ">Large
<option value="X Large">X Large
<option value="XX Large">XX Large</select>
</td></tr></table>
<input type="image" src="https://www.paypal.com/images/sc-but-03.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
<input type="hidden" name="add" value="1">
</form><br>
</body>
</html>

:confused:

tamienne
07-02-2002, 07:34 PM
You can refer to it by number

document.forms[index of form starts at 0]

spgrabel
07-02-2002, 07:40 PM
I tried this and it did not work

document.Forms[0].amount.value="12.00";

joh6nn
07-02-2002, 07:45 PM
<script LANGUAGE="JavaScript1.1">
function ChangeAmount(theForm){

document.forms[theForm].amount.value="12.00";

}


</script>

spgrabel
07-02-2002, 07:54 PM
It did not work. I am sending it correctly from the OnChange event? parameters ok?

joh6nn
07-02-2002, 08:05 PM
sorry, i wasn't paying enough attention. try this:

<script LANGUAGE="JavaScript1.1">
function ChangeAmount(theForm){
theForm.amount.value="12.00";
}


</script>

adios
07-02-2002, 08:42 PM
Every form element has a .form property which refers to the form (object) it's contained in. You referenced it properly when sending it along as an argument:

onChange="ChangeAmount(this.form)">

this refers to the Select object.

When it's received at the other end, it naturally refers to the form itself so, no additional reference is required:

function ChangeAmount(theForm) {
theForm.amount.value="12.00";
}

You only need to use the full path - [window.]document.form_name.element_name - when no object reference is passed to the function. You can probably see why the passed reference approach is more flexible - the form doesn't even need to be named.

Thought you might like an explanation...

spgrabel
07-02-2002, 10:32 PM
Thanks so much for the all of the help today