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
    Regular Coder
    Join Date
    Jul 2002
    Location
    The Netherlands
    Posts
    252
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calculate all values of inputfields

    Hi there,

    I've been searching the web for some time to find a way to calculate the values of a number of input fields. I found that one has to use parseint() to get a numerical value and then you can just add each value.

    Now I would like to create a function that can do this automaticaly for each input field with a certain name. But I just can't seem to get it right.

    My form looks like this, for example:
    Code:
     
    <form name="form">
    <p>Question A1</p>
    <input type="text" name="A1[]" /><br />
    <input type="text" name="A1[]" /><br />
    <input type="text" name="A1[]" /><br />
    
    <p>Question A2</p>
    <input type="text" name="A2[]" /><br />
    <input type="text" name="A2[]" /><br />
    <input type="text" name="A2[]" /><br />
    
    </form>
    Now I would like to sum all the values of the input fields named A1 or A2 using a function:

    Code:
    // Not working by the way ....
    function Sum(name) {
      for(i = 0; i < document.form.name.value.lenght; i++) {
        sum = parseint( ... something here ...); 
      }
      return sum;
    }
    I quess the function has to look something like this, but it doesn't quite works. The problem is that I don not have enough experience with javascript to get the object right.

    I hope someone can help me out with this, or at least give me some tips on which I can get a bit further ...

    Thanks in advance,

    Michiel

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Look into document.getElementsByName()

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    The Netherlands
    Posts
    252
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    I gues I need a bit more help. I've looked up the function getElementsbyName() and it looks like a usefull function. But where do I go next. What do I do with the result of getElementsbyName??

    Using the following form I tested the function sum():

    Code:
    <input type="text" name="E1_0[]" onchange="sum('E1_0')" /><br />
    <input type="text" name="E1_0[]" onchange="sum('E1_0')" /><br />
    <input type="text" name="E1_0[]" onchange="sum('E1_0')" /></td>
    Code:
    function sum(name) {
      test = document.getElementsByName(name);
      alert(test.lenght);
    }
    Unfortunately, all the function alerts is 'undefined' ... If I were to know the number of elements I could setup a for-loop or something ...

    Where to go next??

    Cheers, Michiel

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    <input type="text" name="E1_0[]" onchange="sum(this)" /><br />
    <input type="text" name="E1_0[]" onchange="sum(this)" /><br />
    <input type="text" name="E1_0[]" onchange="sum(this)" />
    ...
    function sum(oFld) {
      var total = 0;
      var flds = oFld.form.elements[oFld.name];
      for (var i = 0; i<flds.length; i++){
        total += Number(flds[i].value);
      }
      alert(total);
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Location
    The Netherlands
    Posts
    252
    Thanks
    0
    Thanked 0 Times in 0 Posts
    glenngv,

    thanks for your help, this is exactly what I was looking for.

    Cheers, Michiel


  •  

    Posting Permissions

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