Reply
Check boxes and radio buttons adding up a total
Old 12-13-2006, 02:04 PM Check boxes and radio buttons adding up a total
Skilled Talker

Posts: 65
Hi

I've got a little script that adds up values for checkboxes to give a total. I'd like to insert a couple of radio buttons in there too. I need to give them a different name to the check boxes, otherwise selecting a radio box deselects the ticks in the checkboxes.

So I currently have
Code:
<script type="text/javascript">
    function checkTotal() {
        document.listForm.total.value = '';
        var sum = 0;
        for (i=0;i<document.listForm.choice.length;i++) {
              if (document.listForm.choice[i].checked) 
{
              sum = sum + parseInt(document.listForm.choice[i].value);
          }
        }
        document.listForm.total.value = sum;
    }
</script>

<form name="listForm">
<input class="checkbox" type="checkbox" name="choice" value="4" onclick="checkTotal()">4
<input class="checkbox" type="checkbox" name="choice" value="5" onclick="checkTotal()">5
<input class="checkbox" type="checkbox" name="choice" value="10" onclick="checkTotal()">10
<input class="radio" type="radio" name="choice1" value="-20" onclick="checkTotal()">-20
<input class="radio" type="radio" name="choice1" value="5" onclick="checkTotal()">5
<input class="checkbox" type="checkbox" name="choice" value="10" onclick="checkTotal()">10
and realise that I need to change it to something vaguely like

Code:
if (document.listForm.choice[i].checked) andif (document.listForm.choice1[i].checked)
{
              sum = sum + parseInt(document.listForm.choice[i].value) + parseInt(document.listForm.choice1[i].value);
except of course that is not valid code.

Any idea what I should be putting please?

thanks

Tony

Last edited by soon : 12-15-2006 at 07:46 AM. Reason: making it clearer
soon is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 12-20-2006, 08:27 AM Re: Check boxes and radio buttons adding up a total
Skilled Talker

Posts: 65
Found the answer - if anyone needs it any time:

Code:
function checkTotal() {
    var total = document.listForm.total;
    var choice = document.listForm.choice;
    var choice1 = document.listForm.choice1;
    var sum = 0;

    for (i=0;i<choice.length;i++) {
        if (choice[i].checked)
            sum = sum + parseInt(choice[i].value);
    }

    for (i=0;i<choice1.length;i++) {
        if (choice1[i].checked)
            sum = sum + parseInt(choice1[i].value);
    }

    total.value = sum;
best wishes

tony
soon is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Check boxes and radio buttons adding up a total
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Webmaster Resources Marketplace:
Software Development Company | Webhosting.UK.com | Text Link Brokers 


   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.11836 seconds with 12 queries