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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Need a JavaScript for adding values...

    I am a newbie at JavaScript. I need to know where I can find a script (or a tutorial on how to build a script) that will sum up values for checked items only.

    For instance... a contractor visits a form page that lists options that can be added to a project on which they wish to bid. I want the page to provide a total for the selected objects only.

    In the future, I may want to link this script to a database so that the visitor can store their session. However, at this time, I simply want the page to provide a total. Can someone please help me with this?

    Thanks ever so much!

    Trish

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So your checkboxes look like this:
    Code:
    ...
    <input type="checkbox" name="project1">p1
    <input type="checkbox" name="project2">p2
    <input type="checkbox" name="project3">p3
    <input type="checkbox" name="project4">p4
    ...
    How do you associate a value with each checkbox? I'm assuming you want to sum some numbers up.. or do you just want to count them?

    How to count:
    Code:
    function countCheckboxes() {
     var count = 0; 
    
     var inputs = document.getElementsByTagName('input');
     for(var i = 0; i < inputs.length; i++) {
      if(inputs.getAttribute('type') == 'checkbox') {
       if(inputs.checked) {
        count++;
       }
      }
     }
    
     return count;
    }
    Hope that helps,
    Sadiq.

  • #3
    New to the CF scene
    Join Date
    Sep 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reiteration...

    Thank you for the input, Sadiq. Actually, I will assign monetary values to the select boxes (I assume that's how any JavaScript will acquire the values it should sum). The script should add the values in order to give a total for what the project would cost.

    Since I will not need to retain any of the information as of yet and I do not need any of the form field info sent to me, I see no need to label the select boxes to reflect the actual item... just the cost of said item.

    Trish


  •  

    Posting Permissions

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