...

View Full Version : From total calculation



Tyipu
05-04-2004, 07: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.

Thanks for help in advance.

Tyip

My script:

<head>

<script>

function getSum(frm){
sum=0;
for(i=0;i<frm.elements.length;i++){
if((frm.elements[i].type=="checkbox" || frm.elements[i].type=="radio") &&

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++){
if((frm.elements[i].type=="checkbox" || frm.elements[i].type=="radio") &&

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

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


function getTotal(?){

?

}

</script>

</head>

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:

sad69
05-04-2004, 10: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,
Sadiq.

Tyipu
05-05-2004, 01:30 AM
Hi Sadiq,

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

sad69
05-05-2004, 01: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++){
if((frm.elements[i].type=="checkbox" || frm.elements[i].type=="radio") && frm.elements[i].checked){
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++){
if((frm.elements[i].type=="checkbox" || frm.elements[i].type=="radio") && frm.elements[i].checked){
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.

Sadiq.

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

:thumbsup:

Take care,

Tyip



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum