[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Javascript calculation problems

Posted on 2007-10-16
5
Medium Priority
?
195 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:kjdavis1210
4 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 total points
ID: 20087850
Name the elements:

<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(optionValues[i].value);
      ntxt=Number(txt)
    }
  }
  var onum=Number(0);
  var totalnum = onum + ntxt;
  ntotalnum   = Math.round(totalnum*100)/100;
  theform.oValues.value = ntotalnum;
}
</script>
The option boxes look like this
<input checked="checked" onclick="createOrder(this.form)" name="optionValues" type="checkbox" value="VALUE" />

<input type="text" NAME="oValues" size="15" class="valueBox">

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 20091321
if your using input fileds with type=checkbox, each needs to have a unique name= attribute
0
 
LVL 2

Expert Comment

by:ll_jaxn
ID: 20110720
<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">
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21046213
Forced accept.

Computer101
EE Admin
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question