Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I prevent saying the same thing in multiple functions?

Status
Not open for further replies.

jovuco

ISP
Oct 30, 2001
17
US
How do I simplify this?

<script>
function calculate1() {
var subtotal = (prefix.form.quantity1.value) * (prefix.form.price1.value);
prefix.form.sum1.value = subtotal;
if (subtotal == &quot;0&quot;){
prefix.form.sum1.value = &quot;&quot;;}
}
function calculate2() {
var subtotal = (prefix.form.quantity2.value) * (prefix.form.price2.value);
prefix.form.sum2.value = subtotal;
if (subtotal == &quot;0&quot;){
prefix.form.sum2.value = &quot;&quot;;}
}
function calculate3() {
var subtotal = (prefix.form.quantity3.value) * (prefix.form.price3.value);
prefix.form.sum3.value = subtotal;
if (subtotal == &quot;0&quot;){
prefix.form.sum3.value = &quot;&quot;;}
}
</script>

<form>
<tr>
<td align=right><input type=&quot;HIDDEN&quot; name=&quot;price1&quot; value=&quot;995&quot;>$995</td>
<td align=right><input type=&quot;text&quot; NAME=&quot;quantity1&quot; onBlur=&quot;calculate1()&quot; SIZE='3'></td>
<td align=right>$<input type=&quot;text&quot; NAME=&quot;sum1&quot; SIZE='5'>
</td>
</tr>
<tr>
<td align=right><input type=&quot;HIDDEN&quot; name=&quot;price2&quot; value=&quot;150&quot;>$150</td>
<td align=right><input type=&quot;text&quot; NAME=&quot;quantity2&quot; onBlur=&quot;calculate2()&quot; SIZE='3'></td>
<td align=right>$<input type=&quot;text&quot; NAME=&quot;sum2&quot; SIZE='5'></td>
</tr>
<tr>
<td align=right><input type=&quot;HIDDEN&quot; name=&quot;price3&quot; value=&quot;595&quot;>$595</td>
<td align=right><input type=&quot;text&quot; NAME=&quot;quantity3&quot; onBlur=&quot;calculate3()&quot; SIZE='3'></td>
<td align=right>$<input type=&quot;text&quot; NAME=&quot;sum3&quot; SIZE='5'>
</td>
</tr>
 
Use a simple loop script :

Code:
<script>
  for (var x=1; x<3; x++) {
    var subtotal = (prefix.form['quantity'+x].value) * (prefix.form['price'+x].value);
    prefix.form['sum'+x].value = subtotal;
    if (subtotal == &quot;0&quot;) {
      prefix.form['sum'+x].value = &quot;&quot;;
    }
  }
</script>
Regards

Big Dave

davidbyng@hotmail.com
 
Actually, this is a better way :

Code:
<script>
function calculate(quantity,price,sum) {
  var subtotal = (document.calc[quantity].value) * (document.calc[price].value);
  document.calc[sum].value = subtotal;
  if (subtotal == &quot;0&quot;) {
    document.calc[sum].value = &quot;&quot;;
  }
}
</script>

<form name=&quot;calc&quot;>
<tr>
<td align=right><input type=&quot;HIDDEN&quot; name=&quot;price1&quot; value=&quot;995&quot;>$995</td>
<td align=right><input type=&quot;text&quot; NAME=&quot;quantity1&quot; onBlur=&quot;calculate('quantity1','price1','sum1')&quot; SIZE='3'></td>
<td align=right>$<input type=&quot;text&quot; NAME=&quot;sum1&quot; SIZE='5'>
</td>
</tr>
<tr>
<td align=right><input type=&quot;HIDDEN&quot; name=&quot;price2&quot; value=&quot;150&quot;>$150</td>
<td align=right><input type=&quot;text&quot; NAME=&quot;quantity2&quot; onBlur=&quot;calculate('quantity2','price2','sum2')&quot; SIZE='3'></td>
<td align=right>$<input type=&quot;text&quot; NAME=&quot;sum2&quot; SIZE='5'></td>
</tr>
<tr>
<td align=right><input type=&quot;HIDDEN&quot; name=&quot;price3&quot; value=&quot;595&quot;>$595</td>
<td align=right><input type=&quot;text&quot; NAME=&quot;quantity3&quot; onBlur=&quot;calculate('quantity3','price3','sum3')&quot; SIZE='3'></td>
<td align=right>$<input type=&quot;text&quot; NAME=&quot;sum3&quot; SIZE='5'>
</td>
</tr>
</form>
Regards

Big Dave

davidbyng@hotmail.com
 
Thanks! I used the second one, it is much easier to add to and understand. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top