Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question From total calculation

    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>


  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    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:
    Code:
    <div id=totalSum>&nbsp;</div>
    Let me know how that works out and if you need any further assistance.

    Hope that helps,
    Sadiq.

  • #3
    New to the CF scene
    Join Date
    May 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Attached Files Attached Files

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    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.

  • #5
    New to the CF scene
    Join Date
    May 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Perfect, thanks very much for the help. I appreciate you taking the time and effort.



    Take care,

    Tyip


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •