• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1829
  • Last Modified:

Need javascript help calculating subtotals on a form

I need some help with javascript.  My script is below.  I have the extended cost fields working properly.  How do I write the javascript to automatically calculate and display the subtotal?

<html>
     <head>
     <SCRIPT language="Javascript">
       function handleEnter (field, event) {
            var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
            if (keyCode == 13) {
                  var i;
                  for (i = 0; i < field.form.elements.length; i++)
                        if (field == field.form.elements[i])
                              break;
                  i = (i + 1) % field.form.elements.length;
                  field.form.elements[i].focus();
                  return false;
            }
            else
            return true;
            }


     function calculate(index){
          var formObject = document.forms[0];
          var quantity = formObject.elements['r'+index+'c1'].value;
          var price = formObject.elements['r'+index+'c2'].value;
          if(quantity > 0 && price > 0){
               formObject.elements['r'+index+'c3'].value = quantity * price;
          }
     }

     </script>
</head>
<body>
<form name="MyForm">
<table cellpadding="0" border="0" ID="Table1">
     <tr>
          <td>ITEM</td>
          <td>QUANTITY</td>
          <td>COST</td>
          <td>EXTENDED COST</td>
     </TR>
     <TR>
          <td><p>Line 1</td>
          <td><input type="text" size="5" id="r1c1" name="r1c1" onchange="calculate(1)" onkeypress="return handleEnter(this, event)"></td>
          <td><input type="text" size="5" id="r1c2" name="r1c2" onchange="calculate(1)" onkeypress="return handleEnter(this, event)"></td>
          <td><input type="text" size="5" id="r1c3" name="r1c3" onkeypress="return handleEnter(this, event)"></td>
     </TR>
     <TR>
          <td><p>Line 2</td>
          <td><input type="text" size="5" id="r2c1" name="r2c1" onchange="calculate(2)" onkeypress="return handleEnter(this, event)"></td>
          <td><input type="text" size="5" id="r2c2" name="r2c2" onchange="calculate(2)" onkeypress="return handleEnter(this, event)"></td>
          <td><input type="text" size="5" id="r2c3" name="r2c3" onkeypress="return handleEnter(this, event)"></td>
     </TR>
     <tr>
      <td>Subtotal</td>
      <td></td>
      <td></td>
                     <td><input type="text" size="5" id=subtot name=subtot></td>
     </tr>
   </table>
</form>
</body>
</html>
0
GCOOK1
Asked:
GCOOK1
1 Solution
 
UnTwstrCommented:
Just loop through all of your values & add them to a var called subtotal. Below is your script that I modified..note the variable called maxID at the top, this limits how many fields are looped through.

<SCRIPT language="Javascript">
       var maxId = 2;  
       function handleEnter (field, event) {
            var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
            if (keyCode == 13) {
                  var i;
                  for (i = 0; i < field.form.elements.length; i++)
                        if (field == field.form.elements[i])
                              break;
                  i = (i + 1) % field.form.elements.length;
                  field.form.elements[i].focus();
                  return false;
            }
            else
            return true;
            }


     function calculate(index){
          var formObject = document.forms[0];
              var subtot = 0;
              for (var i=1;i<=maxId;i++){
                var quantity = formObject.elements['r'+i+'c1'].value;
                  var price = formObject.elements['r'+i+'c2'].value;
                if(quantity > 0 && price > 0){
                           formObject.elements['r'+i+'c3'].value = quantity * price;
                                   subtot = subtot + quantity * price;
                                formObject.elements['subtot'].value = subtot;
                }
            }
     }

     </script>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now