Forum Moderators: open

Message Too Old, No Replies

Basic Form Calculations

         

CelticFC

3:59 pm on Dec 24, 2003 (gmt 0)

10+ Year Member



I have written a basic script to do some simple calculations. The script works fine when placed in a form, if however i cut and paste then change the numbers to perform another calculation it only seems to allow one script\form on the page to work. Any idea what the problem is?

I am a complete novice so it is probably something simple!

Not sure if i am allowed to post the script into this message?

korkus2000

4:10 pm on Dec 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How are you referencing your form elements? Can you place a small snippet of the code here so we can see what you are doing?

CelticFC

5:22 pm on Dec 24, 2003 (gmt 0)

10+ Year Member



Hi korkus2000

Here is a section of the code i have used:

<form>
<script language="LiveScript">

function litres (form){
form.USGallons.value = form.litres.value * 0.2641720
form.UKGallons.value = form.litres.value * 0.2199694
}
</script>
<table>
<tr>
<td width="68">Litres</td>
<td width="90"><input type ="text" name="litres" size=15></td>
</tr>
<tr>
<td><input name="button" type ="button" onClick="litres(this.form)" value="Calculate"></td>
</tr>
<tr>
<td height="25">US Gallons</td>
<td><input name="USGallons" type="text" size=15></td>
</tr>
<tr>
<td height="24">UK Gallons </td>
<td><input name="UKGallons" type="text" size="15"></td>
</tr>
</table>
</form>
</td>

<td width="279" align="left" valign="top"><form>
<script language="LiveScript">

function calc (form){
form.BAR.value = form.psi.value * 0.0689476
}
</script>
<table width="175">
<tr>
<td width="70">PSI</td>
<td width="185"><input type ="text" name="psi" size=15></td>
</tr>
<tr>
<td align="left"><input name="button2" type ="button" onClick="calc(this.form)" value="Calculate"></td>
</tr>
<tr>
<td height="24">BAR</td>
<td><input name="BAR" type="text" size=15></td>
</tr>
</table>
</form></td>
</tr>
<tr>
<td height="20" align="left" valign="top">&nbsp;</td>
<td align="left" valign="top">Convert BAR to PSI </td>
</tr>
<tr>
<td height="89" align="left" valign="top">&nbsp;</td>
<td align="left" valign="top"><form>
<script language="LiveScript">

function calc (form){
form.psi.value = form.bar.value * 14.5037744
}
</script>
<table width="175">
<tr>
<td width="70">BAR</td>
<td width="185"><input type ="text" name="bar" size=15></td>
</tr>
<tr>
<td align="left"><input name="button2" type ="button" onClick="calc(this.form)" value="Calculate"></td>
</tr>
<tr>
<td height="24">PSI</td>
<td><input name="psi" type="text" size=15></td>
</tr>
</table>
</form>

cpear

5:40 pm on Dec 24, 2003 (gmt 0)

10+ Year Member



It looks like all of your functions have the name calc. You need to rename each of them individually. And reference them as such.

Also you should group everything under one script tag section instead of having all your functions all over the place.

cpear

cpear

5:43 pm on Dec 24, 2003 (gmt 0)

10+ Year Member



Also your scritp type should read Javascript not LiveScript.

Live Script is what Netscape originally called Javascript back in the days when it was first being developed.

Cpear

CelticFC

5:54 pm on Dec 24, 2003 (gmt 0)

10+ Year Member



Thanks guys i will try your suggestions out.

Probably on boxing day.

thanks again.