# Totalling up check boxes

I have the following code, and would basically like to calculate a total for the checkboxes checked. I cant use the value= part of the field because this holds a unique ID for the item.

So I was thinking of doing an "onclick add 15 to total field" type thing. Can anyone help me do this, and also recalculate the total when teh checkbox is unchecked?

<form name="form1" method="post" action="">
<p>
<input name="item[]" type="checkbox" id="item[]" value="1">
Item 1<br>
<input name="item[]" type="checkbox" id="item[]" value="2">
Item 2<br>
<input name="item[]" type="checkbox" id="item[]" value="3">
Item 3</p>
<p>Total
<input type="text" name="textfield">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Shouldn't you be using the id property for a unique id?
0
Author Commented:
Oh... wait.. you might actually be right...
0
Author Commented:
Just looked at it, the value has to be the ID so that I can process it using PHP and Arrays.

Ive made some progress, Im using:

<script type="text/javascript">

function reCalc(v, b)
{
var sp = document.getElementById ('total');
if (!b) v = - v;
var t = parseInt(sp.innerHTML);
sp.innerHTML = parseInt(t) + parseInt(v);
}

</script>

and then:

<input type="checkbox" name="array[]" valuex="19.50" value="12" onClick="reCalc(this.valuex, this.checked);">
Total:
&pound;<span id="total">0</span>

Problem is that this doesnt handle decimals, can anyone help?

0
Commented:
try these

<script>
function getTotal(form){
total=0
for (i=0;i<form.elements.length;i++)
if (form.elements[i].id="item[]") total+=form.elements[i].checked
form.textfield.value=total
}
</script>

<form name="form1" method="post" action>
<p><input name="item[]" type="checkbox" id="item[]" value="1"> Item 1<br>
<input name="item[]" type="checkbox" id="item[]" value="2"> Item 2<br>
<input name="item[]" type="checkbox" id="item[]" value="3"> Item 3</p>
<p>Total
<input type="text" name="textfield"><br>
<input type="button" value="get total" onclick="getTotal(this.form)"> </p>
<input type="submit" name="Submit" value="Submit"> </p>
</form>

you could also put the function call on the onclick event of each checkbox to update automatically

<script>
function getTotal(form){
total=0
for (i=0;i<form.elements.length;i++)
if (form.elements[i].id="item[]") total+=form.elements[i].checked
form.textfield.value=total
}
</script>

<form name="form1" method="post" action>
<p><input name="item[]" type="checkbox" id="item[]" value="1"  onclick="getTotal(this.form)"> Item 1<br>
<input name="item[]" type="checkbox" id="item[]" value="2"  onclick="getTotal(this.form)"> Item 2<br>
<input name="item[]" type="checkbox" id="item[]" value="3"  onclick="getTotal(this.form)"> Item 3</p>
<p>Total
<input type="text" name="textfield" value='0'><p>
<input type="submit" name="Submit" value="Submit"> </p>
</form>

0
Commented:
or perhaps it is this that you need ?

<script>
function getTotal(form){
total=0
for (i=0;i<form.elements.length;i++)
if (form.elements[i].checked && form.elements[i].name=="item[]") total+=1*form.elements[i].id
form.textfield.value=total
}
</script>

<form name="form1" method="post" action>
<p><input name="item[]" type="checkbox" id="34" value="1" onclick="getTotal(this.form)">
Item 1<br>
<input name="item[]" type="checkbox" id="56" value="2" onclick="getTotal(this.form)"> Item
2<br>
<input name="item[]" type="checkbox" id="11" value="3" onclick="getTotal(this.form)"> Item
3</p>
<p>Total <input type="text" name="textfield" value="0"></p>
<p><input type="submit" name="Submit" value="Submit"> </p>
</form>
0
Commented:
my last post is the one you want
0
Author Commented:
Hi thats great, just one more thing. Is there a way to show only two decimal places in the total field, with it rounding down if there are more?
0
Commented:
<script>
function getTotal(form){
total=0
for (i=0;i<form.elements.length;i++)
if (form.elements[i].checked && form.elements[i].name=="item[]") total+=1*form.elements[i].id
form.textfield.value=total.toFixed(2)
}
</script>

<form name="form1" method="post" action>
<input name="item[]" type="checkbox" id="34.6767" value="1"onclick="getTotal(this.form)"> Item 1<br>
<input name="item[]" type="checkbox" id="56.553" value="2" onclick="getTotal(this.form)">Item 2<br>
<input name="item[]" type="checkbox" id="11.367" value="3" onclick="getTotal(this.form)">Item 3</p>

<p>Total <input type="text" name="textfield" value="0"></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

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.