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!

question on conditional sum

Status
Not open for further replies.

frippel

IS-IT--Management
Oct 3, 2002
8
BE
I have a form generated dynamicaly with inerHTML with the following structure
| price1 | | tax1 |
| price2 | | tax2 |
etc.

the 1,2,.. are dynamicaly generated
price is a number the user fills in
tax is a selection field and can either be 20% or 0%

What i want is to calculate the sum of all prices where tax=20%

I tried some different options but get no result.

function sum20(frm) {
var price_tot=0;
for(var i=0;bot=frm["tax"+i];i++){
if(bot.value=="20%") {
var intot = frm['price'+i].value * 1;
if(!isNaN(intot)) price_tot = price_tot + intot;
}
}
frm.total20.value = price_tot;
}
 
Um, I don't really understand what your code is supposed to be doing.

Here is a simple test page that might help you see how you can do this kind of thing. If nothing else, you may just have to tweak the getTotals() function.
Code:
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
  <title>Test Harness</title>
  <script type="text/javascript">
    function getTotals(frm)
    {
      var count = 0;
      var l_nTotal = 0;
      while (frm.elements['total'+count] != undefined)
      {
        var l_nAmount = frm.elements['total'+count].value == '' ? 0 : frm.elements['total'+count].value-0;
        var l_nTax = frm.elements['tax'+count].options[frm.elements['tax'+count].selectedIndex].value-0;
        var l_nSubTotal = l_nAmount + l_nAmount * l_nTax;
        l_nTotal += l_nSubTotal;
        frm.elements['subtotal'+count].value = l_nSubTotal;
        count++;
      }
      frm.elements['grandtotal'].value = l_nTotal;
    }
  </script>
</head>
<body>
  <form action="" onsubmit="return false">
  <script type="text/javascript">
    for (var loop = 0; loop < 5; loop++)
    {
      document.write('<input type="text" name="total' + loop + '" value=""> add tax of <select name="tax');
      document.write(loop + '"><option value="0">0%</option><option value="0.2">20%</option>');
      document.write('<option value="0.45">45%</option></select> comes to ');
      document.write('<input type="text" name="subtotal' + loop + '" value="" disabled="disabled"><br>');
    }
  </script>
    <hr>
    <input type="text" name="grandtotal" value="" disabled="disabled">
    <input type="button" value="Calculate Sub totals and Grand total" onclick="getTotals(this.form);">
  </form>
</body>
</html>
If you have any questions about how the code works, just ask.

Let us know how things go.

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Sorry for possible misunderstanding.

I want to calculate the sum of all prices where tax = 20% as a value (or just plain 20)

so for example

price1 = 300 tax1= 20
price2 = 20 tax2=0
price3 = 100 tax3=20
...
price(i)= num tax(i)=num

etc... not a fixed amount of i...

So I would like to add 300 + 100 + ... since their resp. values of tax are 20, and not add price2=20 because tax value is 0

Hope this clears things up a bit...
 
...So I would like to...
Nothing stopping you here. The code is in a working state, all you need to do is modify it [smile]

If you get stuck, let us know what your problem is.

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
I got stuck first script is ok, but gives me still 0 as a result.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top