...

# From total calculation

Tyipu
05-04-2004, 08:49 PM
I have created a form using mostly radio buttons (only one checkbox exception). I have two groups of radio buttons in the form. I can get the total of both groups separately now I need to make them total at the end. The only reason I need to organize them in two groups is that the value of the second group can not be more than 10. I can get the two group values perfectly now I need to add them up somehow. Unfortunately I can not get any further.

Tyip

My script:

<script>

function getSum(frm){
sum=0;
for(i=0;i<frm.elements.length;i++){

frm.elements[i].checked){
sum+=frm.elements[i].value*1;
}
}
document.getElementById("showSum").innerHTML=" "+sum;
}

function getSum2(frm){
sum2=0;
for(i=0;i<frm.elements.length;i++){

frm.elements[i].checked){
sum2+=frm.elements[i].value*1;
}
}
if (sum2>10){
sum2=10
}

document.getElementById("showSum2").innerHTML=" "+sum2;
}

function getTotal(?){

?

}

</script>

And the way I call them in the form tag:

Your points for the first section:<div id=showSum>&nbsp;</div>

Your total points for the last section:<div id=showSum2>&nbsp;</div>

:rolleyes:

05-04-2004, 11:11 PM
Welcome here!

If you could use [ code ] [/ code ] tags (no spaces) around your code, it makes it easier to read.

As for your problem, perhaps something like this:

function trim(s) {
while (s.substring(0,1) == ' ') {
s = s.substring(1,s.length);
}
while (s.substring(s.length-1,s.length) == ' ') {
s = s.substring(0,s.length-1);
}

return s;
}

function getTotal(){
var sum1 = trim(document.getElementById("showSum").innerHTML)*1;
var sum2 = trim(document.getElementById("showSum2").innerHTML)*1;

var ttlSum = sum1 + sum2;

document.getElementById("totalSum").innerHTML = " " + ttlSum;
}

And then make the appropriate div where you want it:

<div id=totalSum>&nbsp;</div>

Let me know how that works out and if you need any further assistance.

Hope that helps,

Tyipu
05-05-2004, 02:30 AM

First of all I would like to say a Big Thank You for your time and effort.

I used your codes but no luck.

I attached my whole site in txt format.

If you have a chance please review it.

Thanks very much.

Tyip

05-05-2004, 02:45 AM
Quite simply, you've declared the getTotal() function, but are never calling it.

So I added a call to getTotal in getSum() and getSum2(). I also found a small bug in my code (if there's no sum for section 1 or section 2, it didn't treat the blank as a 0 but as a NaN), so I've fixed that.

Here's the code:

<script>

function getSum(frm){
sum=0;
for(i=0;i<frm.elements.length;i++){
sum+=frm.elements[i].value*1;
}
}
document.getElementById("showSum").innerHTML=" "+sum;

getTotal();
}

function getSum2(frm){
sum2=0;
for(i=0;i<frm.elements.length;i++){
sum2+=frm.elements[i].value*1;
}
}

if (sum2>10){
sum2=10
}

document.getElementById("showSum2").innerHTML=" "+sum2;

getTotal();
}

function trim(s) {
while (s.substring(0,1) == ' ') {
s = s.substring(1,s.length);
}
while (s.substring(s.length-1,s.length) == ' ') {
s = s.substring(0,s.length-1);
}

return s;
}

function getTotal(){
var sum1 = trim(document.getElementById("showSum").innerHTML)*1;
var sum2 = trim(document.getElementById("showSum2").innerHTML)*1;

sum1 = isNaN(sum1) ? 0 : sum1;
sum2 = isNaN(sum2) ? 0 : sum2;

var ttlSum = sum1 + sum2;

document.getElementById("totalSum").innerHTML = " " + ttlSum;
}

</script>

That should do it. Let me know how you make out.

Tyipu
05-05-2004, 03:06 AM
Perfect, thanks very much for the help. I appreciate you taking the time and effort.

:thumbsup:

Take care,

Tyip