Link to home
Start Free TrialLog in
Avatar of kjdavis1210
kjdavis1210

asked on

Javascript calculation problems

I have the script below, it takes a number of check boxes that are options with prices to a main number for example I have a main car price, and adding three different option groups to give me the new total.

<script type="text/javascript">
                            function createOrder()
                            {
                            optionValues=document.forms[0].optionValues
                            txt=0
                            for (i=0;i<optionValues.length;++ i)
                            {
                            if (optionValues[i].checked)
                            {
                            txt=Number(txt) + Number(optionValues[i].value)
                            ntxt=Number(txt)
                            }
                            }
                            onum=Number(0)
                            totalnum    = onum + ntxt
                            ntotalnum   = Math.round(totalnum*100)/100;
                            document.getElementById("oValues").value = ntotalnum
                            }
                            </script>
The option boxes look like this
<input checked="checked" onclick="createOrder()" name="optionValues" type="checkbox" value=VALUE />
Then there is the box that gets written the value
<input type="text" id="oValues" size="15" class="valueBox">

This code works great, it is clean and fast to update. I just have 1 main problems.
With that code and having all checkboxes named the same I can not pass this value on in a form element to another page.

Any ideas how to rewrite the code so that the element names are different so I can pass them through the form. I have tried a number or things and when I do it, it will not pass any values.

Kevin
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if your using input fileds with type=checkbox, each needs to have a unique name= attribute
Avatar of ll_jaxn
ll_jaxn

<script type="text/javascript">
function createOrder(theForm)  {
  var optionValues=theForm.optionValues;
  var txt=0
  for (var i=0;i<optionValues.length;++ i) {
    if (optionValues[i].checked) {
      txt=Number(txt) + Number(documentGetElementByID("v"+optionValues[i].ID).value);
      ntxt=Number(txt)
    }
  }
  var onum=Number(0);
  var totalnum = onum + ntxt;
  ntotalnum   = Math.round(totalnum*100)/100;
  theform.oValues.value = ntotalnum;
}
</script>
<form onsubmit(return createOrder(this))>
<input checked="checked" name="optionValues" id='o1'  type="checkbox" value="VALUE" />
<input type="text" NAME="vo1"  size="15" class="valueBox">
Forced accept.

Computer101
EE Admin