Checkbox with Parsed Value amount

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
dreagose
Forum Newbie
Posts: 15
Joined: Wed Sep 02, 2009 9:28 am

Checkbox with Parsed Value amount

Post by dreagose »

I need help with some code. I have a checkbox form with Quantity and I need it to total at the bottom. I can get the code to do just a checkbox with a set value but since the value has more data than just a total I need it parsed. Also, I cant get the total to work with both Qty(textbox) and checkbox changing.

The value of the checkbox will follow this format: "acct^partn^desc^price^qty^na^na^na^ship weight"
the sections between qty and ship weight, labeled "na" will 99.99% of the time not be used but still has to be sent as an empty value -
example: "acc1t^abc123^blahblah^10.15^1^^^^5"

:banghead: <---- Me

here is what I have:

Code: Select all

<script type="text/javascript">
    function updatetotal() {
        document.main.total.value = '';
        var sum = 0;
        
          for (i=0;i<document.main.choice.length;i++) {
          if (document.main.choice[i].checked) {
            
            // value is acct, partn, desc, price, qty, na, na, na, ship weight
            var ar = new String(document.main.choice[i].value);
            var ara = Array(ar.split('^'));
            var val = ara(3);
            var qty = ara(4);
            sum = sum + parseInt(val * qty);
            ar = document.main.choice[i].value;
          }
        }
        document.main.total.value = sum;
        document.main.tv.value = val;
        document.main.tq.value = qty;
    }
    </script>
 
<form name="main">
    <p>test a: Qty: <input type='TEXT' name='op1' value='1' size='2' onchange="updatetotal()" /> 
    Select:
    <input type="checkbox" name="choice1" value="acc1t^abc123^desc blah1^20.10^op1^^^^14" onchange="updatetotal()"/>$20.10<br/>
        test b: Qty: <input type='TEXT' name='op2' value='1' size='2' onchange="updatetotal()" />
         Select:
         <input type="checkbox" name="choice2" value="acct1^abc124^desc blah2^30.20^op2^^^^24" onchange="updatetotal()"/>$30.20<br/>
        test c: Qty: <input type='TEXT' name='op3' value='1' size='2' onchange="updatetotal()" />
        Select:
        <input type="checkbox" name="choice3" value="acct1^abc125^desc blah3^40.30^op3^^^^34" onchange="updatetotal()"/>$40.30<br/>
        test d: Qty: <input type='TEXT' name='op4' value='1' size='2' onchange="updatetotal()" />
        Select:
        <input type="checkbox" name="choice4" value="acct1^abc126^desc blah4^50.40^op4^^^^44" onchange="updatetotal()"/>$50.40<br/>
    </p>
    <p>Total: <input type="text" size="5" name="total" value="0"/> / 
        test a: <input type="text" size="5" name="tv" value="0"/>
        test b: <input type="text" size="5" name="tq" value="0"/>
    </p>
</form>
dreagose
Forum Newbie
Posts: 15
Joined: Wed Sep 02, 2009 9:28 am

Re: Checkbox with Parsed Value amount

Post by dreagose »

ok, found my own solution...
I had another problem and that was the NAME field was also going to be different for every option so I had to use the ID field.

ANY improvements will be appreciated though...

Code: Select all

<script type="text/javascript">
    
    function updatetotal() {
        document.main.total.value = '';
        //var qtyid = document.getElementById(qty);
        //var selid = document.getElementById(sel);
        var sum = 0;
        var val = 0;
        var qty = 0;
        var ar = "none";
        for (cnt=0;cnt<document.main.sel.length;cnt++) {
          if (document.main.sel[cnt].checked) {
            var ar = new String(document.main.sel[cnt].value);
            var qty = parseInt(document.main.qty[cnt].value);
            var ara = ar.split('^');
            var val = ara[3];
            sum = sum + val * qty;
            // sum = sum + parseInt(document.main.choice[cnt].value);
          }
        }
        document.main.total.value = sum;
    }
</script>
 
 
<form name="main">
    <p>test a: Qty: <input type='TEXT' name='op1' id="qty" value='1' size='2' onblur="updatetotal()"/> 
    Select:
    <input type="checkbox" name="choice1" id="sel" value="acc1t^abc123^desc blah1^20.10^op1^^^^14" onchange="updatetotal()"/>$20.10<br/>
        test b: Qty: <input type='TEXT' name='op2' id="qty" value='1' size='2' onblur="updatetotal()"/>
         Select:
         <input type="checkbox" name="choice2" id="sel" value="acct1^abc124^desc blah2^30.20^op2^^^^24" checked="checked" onchange="updatetotal()"/>$30.20<br/>
        test c: Qty: <input type='TEXT' name='op3' id="qty" value='1' size='2' onblur="updatetotal()"/>
        Select:
        <input type="checkbox" name="choice3" id="sel" value="acct1^abc125^desc blah3^40.30^op3^^^^34" onchange="updatetotal()"/>$40.30<br/>
        test d: Qty: <input type='TEXT' name='op4' id="qty" value='1' size='2' onblur="updatetotal()"/>
        Select:
        <input type="checkbox" name="choice4" id="sel" value="acct1^abc126^desc blah4^50.40^op4^^^^44" onchange="updatetotal()"/>$50.40<br/>
    </p>
    <p>Total: <input type="text" size="5" name="total" value="0"/></p>
</form>
 
I'm sure the code is sloppy for most standards but it works... :D
Post Reply