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 2 of 2
  1. #1
    pml
    pml is offline
    New Coder
    Join Date
    Mar 2005
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How can I use an onclick event to calculate figures in form elements?

    I have some problems with these objects. I want the checked items to be added up and the sum displayed at the end. Unfortunately I don't have a good "Calculate" function for this purpose and hope maybe you've got an idea about it. The code should preferebly be dynamic, e.g. the code shouldn't mind whether there is four objects (as in this case) or 15 more radio groups and an additional 10 checkboxes. When the URL is loaded the default sum should also be displayed (4). Is it possible to achieve that?

    <html>
    <head>

    <script language="javascript" type="text/javascript">
    function Calculate (number) {


    }
    </script>
    </head>

    <body>
    <p>

    <input name="RadioGroup1" type="radio" value="SomeValue1" checked onClick="Calculate(1)"> 1 <br>
    <input name="RadioGroup1" type="radio" value="SomeValue2" onClick="Calculate(2)"> 2 <br>
    <input name="RadioGroup1" type="radio" value="SomeValue3" onClick="Calculate(3)"> 3 <br>
    <input name="check1" type="checkbox" id="check1" value="SomeValue4" onClick="Calculate(8)"> 8</p>
    <p>

    <input name="RadioGroup2" type="radio" value="SomeValue5" checked onClick="Calculate(3)"> 3 <br>
    <input name="RadioGroup2" type="radio" value="SomeValue6" onClick="Calculate(7)"> 7 <br>
    <input name="RadioGroup2" type="radio" value="SomeValue7" onClick="Calculate(9)"> 9 <br>
    <input name="check2" type="checkbox" value="SomeValue8" onClick="Calculate(12)"> 12 </p>
    <p>The sum of the radio buttons and checkboxes is:
    <script language="javascript" type="text/javascript">

    document.write.??????????????


    }
    </script>
    </p>


    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,467
    Thanks
    3
    Thanked 495 Times in 482 Posts
    not sure I understand but

    PHP Code:
    <html>
    <
    head>

    <
    script language="javascript" type="text/javascript">
    function 
    Calculate (obj) {
     
    ips=obj.parentNode.getElementsByTagName('INPUT');
     
    sum=0;
     for (
    i=0;i<ips.length;i++){
      if (
    ips[i].checked){
       
    sum+=ips[i].value*1;
      }
     }
     
    document.getElementById('total').value=sum;
    }
    </script>
    </head>

    <body>
    <p>
    <input name="RadioGroup1" type="radio" value="1" onClick="Calculate(this)"> 1 <br>
    <input name="RadioGroup1" type="radio" value="2" onClick="Calculate(this)"> 2 <br>
    <input name="RadioGroup1" type="radio" value="3" onClick="Calculate(this)"> 3 <br>
    <input name="check1" type="checkbox" id="check1" value="4" onClick="Calculate(this)"> 4
    <br>
    <br>
    <input name="RadioGroup2" type="radio" value="5" onClick="Calculate(this)"> 5 <br>
    <input name="RadioGroup2" type="radio" value="6" onClick="Calculate(this)"> 6 <br>
    <input name="RadioGroup2" type="radio" value="7" onClick="Calculate(this)"> 7 <br>
    <input name="check2" type="checkbox" value="8" onClick="Calculate(this)"> 8
    </p>
    <p>
    The sum of the radio buttons and checkboxes is:<input id="total" size="10">
    </p>


    </body>
    </html> 


  •  

    Posting Permissions

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