Getting correct value from radio button

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bernie

    Getting correct value from radio button

    Hello all,

    I am new to the group and new to javascripting, so I am hoping to find
    some good help here.

    Here is a snippet of my code:

    HTML:
    Name: <input type="text" name="ageName0" id="ageName0" />
    <input type="radio" value="0" name="age0" onclick="getPri ce();" /6
    and under
    <input type="radio" value="20" name="age0" onclick="getPri ce();" /7
    to 12
    <input type="radio" value="50" name="age0" onclick="getPri ce();" /13
    and over
    Amount: $ <input type="text" name="ageAmount 0" id="ageAmount0 " /><br>
    ....
    ....
    Name: <input type="text" name="ageName6" id="ageName6" />
    <input type="radio" value="0" name="age6" onclick="getPri ce();" /6
    and under
    <input type="radio" value="20" name="age6" onclick="getPri ce();" /7
    to 12
    <input type="radio" value="50" name="age6" onclick="getPri ce();" /13
    and over
    Amount: $ <input type="text" name="ageAmount 6" id="ageAmount6 " /><br>


    Javascript:
    var allAges = new
    Array(thisAge0, thisAge1,thisAg e2,thisAge3,thi sAge4,thisAge5, thisAge6);
    var allAgesAmounts = new
    Array(thisAgeAm ount0,thisAgeAm ount1,thisAgeAm ount2,thisAgeAm ount3,thisAgeAm ount4,thisAgeAm ount5,thisAgeAm ount6);

    function getPrice()
    {
    for (var i = 0; i < allAges.length; i++)
    {
    allAgesAmounts[i].value = "";
    for (var j = 0; j < allAges[i].length; j++)
    {
    if (allAges[i][j].checked == true)
    {
    allAgesAmounts[i].value = allAges[i][j].value;
    console.log(all AgesAmounts[i].value);
    }
    }
    }
    ....
    ....
    }

    The issue I am having is that when I click on the first set of radio
    buttons, I am presented with 1 value, but if I click on the second set
    of radio button, I am presented with 2 values instead of 1. The same
    goes for the third, fourth, fifth and sixth where I a presented with
    3, 4, 5 and 6 values, respectively.

    You can see what I am referring to by going to this link
    http://www.wootenfamilyreunion.org/6a.php.

    Is there a way to stop this from happening?

    Thanks for your time and patience.
  • SAM

    #2
    Re: Getting correct value from radio button

    Bernie a écrit :
    Hello all,
    >
    I am new to the group and new to javascripting, so I am hoping to find
    some good help here.
    <form action="#">
    Name: <input type="text" name="ageName0" id="ageName0" />
    <input type="radio" value="0" name="age0" onclick="getPri ce(this);" /6
    and under
    <input type="radio" value="20" name="age0" onclick="getPri ce(this);" /7
    to 12
    <input type="radio" value="50" name="age0" onclick="getPri ce(this);" /13
    and over
    Amount: $ <input type="text" name="ageAmount 0" id="ageAmount0 " /><br>

    .....

    Your Total = <input name="totalAmou nt" id="totalAmont" ><br>
    <input type=reset><inp ut type=submit>
    </form>


    JS :
    ====

    function getPrice(what) {
    var f = what.form; // which form it is
    var age = what.name; // name of the radio
    what = f[age]; // collection of radios
    age = age.substring(a ge.indexOf('e') +1); // number (index)
    for(var i=0; i<what.length; i++)
    if(what[i].checked) f['ageAmount'+age].value = what[i].value ;
    total(f);
    }
    function total(f) {
    var t = 0, L = (f.length-3)/5; // or L = 6 for your example
    for(var i=0; i<L; i++) t += f['ageAmount'+i].value*1;
    f.totalAmount.v alue = t;
    }

    The issue I am having is that when I click on the first set of radio
    buttons, I am presented with 1 value, but if I click on the second set
    of radio button, I am presented with 2 values instead of 1. The same
    goes for the third, fourth, fifth and sixth where I a presented with
    3, 4, 5 and 6 values, respectively.
    the values of inputs are texts (type=text) and not numbers
    so you add the values as texts
    You must to convert them in numbers

    var price = foo1.value *1 + foo2.value*1 + foo3.value*1;

    --
    sm

    Comment

    • Bernie

      #3
      Re: Getting correct value from radio button

      On Jun 17, 2:50 pm, SAM <stephanemoriau x.NoAd...@wanad oo.fr.invalid>
      wrote:
      Bernie a écrit :
      >
      Hello all,
      >
      I am new to the group and new to javascripting, so I am hoping to find
      some good help here.
      >
      <form action="#">
      Name: <input type="text" name="ageName0" id="ageName0" />
      <input type="radio" value="0" name="age0" onclick="getPri ce(this);" /6
      and under
      <input type="radio" value="20" name="age0" onclick="getPri ce(this);" /7
      to 12
      <input type="radio" value="50" name="age0" onclick="getPri ce(this);" /13
      and over
      Amount: $ <input type="text" name="ageAmount 0" id="ageAmount0 " /><br>
      >
      ....
      >
      Your Total = <input name="totalAmou nt" id="totalAmont" ><br>
      <input type=reset><inp ut type=submit>
      </form>
      >
      JS :
      ====
      >
      function getPrice(what) {
         var f = what.form;    // which form it is
         var age = what.name;  // name of the radio
         what = f[age];        // collection of radios
         age = age.substring(a ge.indexOf('e') +1);  // number (index)
         for(var i=0; i<what.length; i++)
         if(what[i].checked) f['ageAmount'+age].value = what[i].value ;
         total(f);}
      >
      function total(f) {
        var t = 0, L = (f.length-3)/5;  // or L = 6 for your example
        for(var i=0; i<L; i++) t += f['ageAmount'+i].value*1;
        f.totalAmount.v alue = t;
      >
      }
      The issue I am having is that when I click on the first set of radio
      buttons, I am presented with 1 value, but if I click on the second set
      of radio button, I am presented with 2 values instead of 1. The same
      goes for the third, fourth, fifth and sixth where I a presented with
      3, 4, 5 and 6 values, respectively.
      >
      the values of inputs are texts (type=text) and not numbers
      so you add the values as texts
      You must to convert them in numbers
      >
      var  price = foo1.value *1 + foo2.value*1 + foo3.value*1;
      >
      --
      sm
      Thanks for replying back, SM.
      My next question to you is in the line - "var t = 0, L = (f.length-3)/
      5; // or L = 6 for your example", where does the 3 and 5 come from?
      What are they referencing? I understand that f.length is the total
      amount of element in the form, but the 3 and 5 has me stumped.

      Thanks

      B

      Comment

      • SAM

        #4
        Re: Getting correct value from radio button

        Bernie a écrit :
        >
        Thanks for replying back, SM.
        My next question to you is in the line - "var t = 0, L = (f.length-3)/
        5; // or L = 6 for your example", where does the 3 and 5 come from?
        What are they referencing? I understand that f.length is the total
        amount of element in the form, but the 3 and 5 has me stumped.
        you have 5 inputs by line
        you would have to have 3 inputs in end of your form
        - total
        - reset
        - submit

        L = the form length (numbers of its elements (input, textarea) )

        As you have a sub-total in each line in 5th position
        I do a loop (L-3)/5 times

        If that disturb you, you can loop on the whole form
        searching elements with 'ageAmount' in their names :

        var t = 0;
        for(var i=0, L=f.length; i<L; i++) {
        if(f[i].name.indexOf(' ageAmount')>=0) t += f[i].value*1:
        }


        does the name content 'ageAmount' ?
        name.indexOf('a geAmount')>=0

        --
        sm

        Comment

        • Bernie

          #5
          Re: Getting correct value from radio button

          On Jun 18, 3:04 pm, SAM <stephanemoriau x.NoAd...@wanad oo.fr.invalid>
          wrote:
          Bernie a écrit :
          >
          >
          >
          Thanks for replying back, SM.
          My next question to you is in the line - "var t = 0, L = (f.length-3)/
          5;  // or L = 6 for your example", where does the 3 and 5 come from?
          What are they referencing?  I understand that f.length is the total
          amount of element in the form, but the 3 and 5 has me stumped.
          >
          you have 5 inputs by line
          you would have to have 3 inputs in end of your form
          - total
          - reset
          - submit
          >
          L = the form length (numbers of its elements (input, textarea) )
          >
          As you have a sub-total in each line in 5th position
          I do a loop  (L-3)/5  times
          >
          If that disturb you, you can loop on the whole form
          searching elements with 'ageAmount' in their names :
          >
          var t = 0;
          for(var i=0, L=f.length; i<L; i++) {
             if(f[i].name.indexOf(' ageAmount')>=0) t += f[i].value*1:
             }
          >
          does the name content 'ageAmount' ?
             name.indexOf('a geAmount')>=0
          >
          --
          sm
          No, that did not disturb me. I was just curious. Thanks again.

          Comment

          Working...