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.form s[0].optio nValues
txt=0
for (i=0;i<optionValues.length ;++ i)
{
if (optionValues[i].checked)
{
txt=Number(txt) + Number(optionValues[i].val ue)
ntxt=Number(txt)
}
}
onum=Number(0)
totalnum = onum + ntxt
ntotalnum = Math.round(totalnum*100)/1 00;
document.getElementById("o Values").v alue = 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
<script type="text/javascript">
function createOrder()
{
optionValues=document.form
txt=0
for (i=0;i<optionValues.length
{
if (optionValues[i].checked)
{
txt=Number(txt) + Number(optionValues[i].val
ntxt=Number(txt)
}
}
onum=Number(0)
totalnum = onum + ntxt
ntotalnum = Math.round(totalnum*100)/1
document.getElementById("o
}
</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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if your using input fileds with type=checkbox, each needs to have a unique name= attribute
<script type="text/javascript">
function createOrder(theForm) {
var optionValues=theForm.optio nValues;
var txt=0
for (var i=0;i<optionValues.length; ++ i) {
if (optionValues[i].checked) {
txt=Number(txt) + Number(documentGetElementB yID("v"+op tionValues [i].ID).va lue);
ntxt=Number(txt)
}
}
var onum=Number(0);
var totalnum = onum + ntxt;
ntotalnum = Math.round(totalnum*100)/1 00;
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">
function createOrder(theForm) {
var optionValues=theForm.optio
var txt=0
for (var i=0;i<optionValues.length;
if (optionValues[i].checked) {
txt=Number(txt) + Number(documentGetElementB
ntxt=Number(txt)
}
}
var onum=Number(0);
var totalnum = onum + ntxt;
ntotalnum = Math.round(totalnum*100)/1
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
Computer101
EE Admin