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 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2013
    Posts
    44
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Total Form Input Checkbox

    I have a table with 5 rows consisting of 5 input boxes that a user will enter a number in and i have 5 checkboxes in the next column

    I want to be able to total based on wether the checkbox is checked



    Code:
    function total_credit_payoff()
    {
    if document.getElementById("debtpayoff1").checked{
     othercredit1 = parseFloat(document.getElementById("credC1").value);
     othercredit2 = parseFloat(document.getElementById("credC2").value);
     othercredit3 = parseFloat(document.getElementById("credC3").value);
     othercredit4 = parseFloat(document.getElementById("credC4").value);
     auto1 = parseFloat(document.getElementById("auto1").value);
     totalcreditbal = othercredit1 + othercredit2 + othercredit3 + othercredit4 + auto1
    }
    In other words if the checkbox next to the input field is checked then the amount gets added to the total otherwise if the box is NOT checked it does not get added

    Thanks
    Last edited by billboy; 02-11-2013 at 08:34 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,309
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Here you are:-

    Code:
    <html>
    <head>
    </head>
    <body>
    
    <form id = "myform">
    <input type = "text" name = "txt1"><input type = "checkbox" name= "chk1" ><br>
    <input type = "text" name = "txt1"><input type = "checkbox" name= "chk1" ><br>
    <input type = "text" name = "txt1"><input type = "checkbox" name= "chk1" ><br>
    <input type = "text" name = "txt1"><input type = "checkbox" name= "chk1"><br>
    <input type = "text" name = "txt1"><input type = "checkbox" name= "chk1" ><br>
    
    <input type = "button" value = "Get Total" onclick = "addemup()">
    </form>
    
    <script type = "text/javascript">
    
    function addemup() {
    var total = 0;
    var count = 0;
    var f = document.getElementById("myform");
    for (var i=0; i<f.chk1.length; i++) {
    if (f.chk1[i].checked == true) {
    var x = f.txt1[i].value *1 || 0;   // 0 if a NaN entered
    f.txt1[i].value = x;  // write it back to the field
    total = total + x; 
    count++;
    }
    }
    
    if (count == 1) {
    alert ("The value of the one textbox you have checked is " + total);
    }
    else if (count>1) {
    alert ("The total of the " + count + " textboxes you have checked is " + total);
    }
    }
    
    </script>
    
    </body>
    </html>

    Looking after up to six kids will be hard. Staff have only two pairs of hands, after all. - Presenter BBC Radio 2
    Last edited by Philip M; 02-11-2013 at 09:23 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Feb 2013
    Posts
    44
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks I worked it out with long series of If Else statements
    but I new there had to be a better way

    Your way is much cleaner and a good function I can learn from and implement in other parts of my program.

    Thank you so much for taking the time to help, it is greatly appreciated

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,309
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by billboy View Post
    Thank you so much for taking the time to help, it is greatly appreciated
    Glad to be able to help!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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