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
    Jun 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    form totalling problem

    I have a form with a bunch of text inputs and some other inputs that are checkboxes with numeric values.

    I use the following which works great until I have the text inputs:

    [CODE]

    function startCalc(){
    interval = setInterval("calc()",1);
    }
    function calc(){
    var elems = document.forms['membership'].elements;
    var total = 0;
    for(var i=0;i<elems.length;i++) {
    if (elems[i].checked) {total += +(elems[i].value);}
    }
    document.forms['membership'].elements['total'].value = total;
    }
    function stopCalc(){
    clearInterval(interval);
    }

    [CODE]

    What is the best way to only process named elements rather than all of the elements?

    TIA, Jeff

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Why would you base this on an interval??? And an interval of ONE millisecond, no less????

    Since you only care about checkboxes, only do the calc when a checkbox is clicked on.

    Note that just looking for the .checked property is *NOT* good enough: radio buttons *also* have a checked property. So:

    Code:
    <html>
    <head>
    <script>
    function calc( )
    {
        var frm = document.membership;
        var total = 0.0;
        for ( var e = 0; e < frm.elements.length; ++e )
        {
            var fld = frm.elements[e];
            if ( fld.type == "checkbox" && fld.checked ) 
            {
                total += parseFloat( fld.value );
            }
        }
        frm.total.value = total;
    }
    </script>
    </head>
    <body>
    <form name="membership">
    <input name="phoo"><p>
    <input type=checkbox name="CB" value="1.334" onclick="calc();"> 1.334
    <input type=checkbox name="CB" value="2" onclick="calc();"> 2
    <input type=checkbox name="CB" value="44" onclick="calc();"> 44
    <p>
    <input type=checkbox name="soloCB" value="12" onclick="calc();"> 12
    <p>
    <input type=radio name="RB" value="1"> 1
    <input type=radio name="RB" value="2"> 2
    <p>
    Total: <input name="total" readonly>
    </form>
    </body>
    </html>

  • #3
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    js total

    Thanks, That makes more sense to just identify the type.

    So to add radios I could just use:

    if ((fld.type == "checkbox" || fld.type =="radio) && fld.checked ))

    And then add the function call to the radios?

    Jeff
    Last edited by jmurch; 03-08-2009 at 01:57 AM.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Actually, if you want to include the radio buttons, too, you could just do:
    Code:
    if ( fld.checked != null && fld.checked )
    That's because ONLY checkboxes and radio buttons *have* a checked property. It will be null for all other types.

    But of course nothing wrong with doing it as you showed.

    Yes, if you wanted to add radio button values to the calculation, you'd need to put the onclick handler in them, as well.

  • #5
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks that all worked great!.

    Jeff


  •  

    Posting Permissions

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