/    Sign up×
Community /Pin to ProfileBookmark

How to calculate totals in more than one field…

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…

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@David_HarrisonJul 23.2003 — Try this calculator, I arranged contents in a simple table to make it easier to read the code.

[upl-file uuid=0c464cbe-6854-4c83-b884-ff369fd83042 size=2kB]ksp.txt[/upl-file]
Copy linkTweet thisAlerts:
@kspauthorJul 23.2003 — Thank you so much! Works perfectly, and I understand what you've done. I appreciate the time you took to help. ?
Copy linkTweet thisAlerts:
@David_HarrisonJul 23.2003 — Happy to help. ?
×

Success!

Help @ksp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...