Click to See Complete Forum and Search --> : How to calculate totals in more than one field...


ksp
07-23-2003, 12:30 PM
Hi,

I have a listing of products with these headings:

Qty. Product Retail Price Wholesale Price Total Retail Price Total Wholesale Price ADD

I have the javascript set up so that when someone enters the quantity and clicks ADD, the Retail Price x Quanity and Wholesale Price x Quantity are calculated and the values are entered in the Total Retail Price field and the Total Wholesale Price field.

I can do this successfully for one item, but when I add the coding for two items, then it doesn't work at all. How can I fix this so that any number of the items can be calculated?

Here's the javascript (I realize I haven't taken care of rounding to two places for the dollar amounts; I just wanted to get the basic script working correctly first):

<script language="javascript">

<!-- Hide -->
function determineRWPrices() {

var drPrice = window.document.mProducts.drPrice.value;
var dwPrice = window.document.mProducts.dwPrice.value;
var qty = window.document.mProducts.qty.value;

var rPrice = drPrice * qty;
var wPrice = dwPrice * qty;

window.document.mProducts.rPrice.value = rPrice;
window.document.mProducts.wPrice.value = wPrice;
}

// End hiding
</script>

and the html code:

<tr>
<td><input name="qty" type="text" id="qty" size="3" maxlength="4"></td>
<td>100</td>
<td>Product 1</td>
<td><input type="hidden" name="drPrice" value="4.99">
$4.99</td>
<td><input type="hidden" name="dwPrice" value="3.24">
$3.24</td>
<td><input name="rPrice" type="text" id="rPrice" size="6"></td>
<td><input name="wPrice" type="text" id="wPrice" size="6"></td>
<td><input type="button" value="ADD" onClick="determineRWPrices(); return false;" name="button"></td>
</tr>

As I said, it works fine with just one like above, but when I code a second product, zilch.

Any help is very much appreciated...

David Harrison
07-23-2003, 01:17 PM
Try this calculator, I arranged contents in a simple table to make it easier to read the code.

ksp
07-23-2003, 01:31 PM
Thank you so much! Works perfectly, and I understand what you've done. I appreciate the time you took to help. :)

David Harrison
07-23-2003, 01:46 PM
Happy to help. :)