Radio Button Loop inside Form Element Loop

Discussion in 'Javascript' started by wreed, Oct 13, 2006.

  1. wreed

    wreed Guest

    I have a for loop seen below....

    var the_form = document.getElementById(formName);
    for(var i=0; i<the_form.length; i++)
    {
    var temp = the_form.elements.type;
    if (temp == "radio")
    {
    for (x = 0; x < the_form.elements.length - 1; x++)
    {
    //do stuff
    }
    }
    }
    Right before the inside loop I do the following and next to its the
    results, any idea why the last one returns 0?
    alert(the_form.elements.id); // returns question1
    alert(the_form.question1.length); // returns 4 (amount of radio
    buttons in that group)
    alert(the_form.elements.length); // returns 0

    I cannot figure out why this is the case. Am I blatantly looking over
    something?
    wreed, Oct 13, 2006
    #1
    1. Advertising

  2. wreed

    Guest

    wreed wrote:
    > I have a for loop seen below....
    >
    > var the_form = document.getElementById(formName);
    > for(var i=0; i<the_form.length; i++)
    > {
    > var temp = the_form.elements.type;
    > if (temp == "radio")
    > {
    > for (x = 0; x < the_form.elements.length - 1; x++)
    > {
    > //do stuff
    > }
    > }
    > }
    > Right before the inside loop I do the following and next to its the
    > results, any idea why the last one returns 0?
    > alert(the_form.elements.id); // returns question1
    > alert(the_form.question1.length); // returns 4 (amount of radio
    > buttons in that group)
    > alert(the_form.elements.length); // returns 0
    >
    > I cannot figure out why this is the case. Am I blatantly looking over
    > something?


    What exactly are you trying to accomplish here?
    , Oct 13, 2006
    #2
    1. Advertising

  3. wreed

    wreed Guest

    wrote:
    > What exactly are you trying to accomplish here?


    Well I am using an AHAH library (flavor of Ajax) to do my loading of
    content into div's. In this case I am adding in form validation and
    the way the library is written I have to do some validation this way.
    Basically I am trying to tell if a radio button in the list is selected
    and I want to add some HTML into a span if no radio button is selected.
    Does that make sense?
    wreed, Oct 13, 2006
    #3
  4. wreed

    wreed Guest

    wreed wrote:
    > wrote:
    > > What exactly are you trying to accomplish here?

    >
    > Well I am using an AHAH library (flavor of Ajax) to do my loading of
    > content into div's. In this case I am adding in form validation and
    > the way the library is written I have to do some validation this way.
    > Basically I am trying to tell if a radio button in the list is selected
    > and I want to add some HTML into a span if no radio button is selected.
    > Does that make sense?


    OOps I am basically trying to loop through each radio button list ( I
    have more than one) in this form elements collection.
    wreed, Oct 13, 2006
    #4
  5. wreed

    Guest

    wreed wrote:
    > wreed wrote:
    > > wrote:
    > > > What exactly are you trying to accomplish here?

    > >
    > > Well I am using an AHAH library (flavor of Ajax) to do my loading of
    > > content into div's. In this case I am adding in form validation and
    > > the way the library is written I have to do some validation this way.
    > > Basically I am trying to tell if a radio button in the list is selected
    > > and I want to add some HTML into a span if no radio button is selected.
    > > Does that make sense?

    >
    > OOps I am basically trying to loop through each radio button list ( I
    > have more than one) in this form elements collection.


    one way to do it would be to just loop over the radio button's
    themselves:

    <script type="text/javascript">
    function test() {
    q1Radios = document.the_form.question1;
    for (x=0; x < q1Radios.length; x++) {
    if ( q1Radios[x].checked )
    alert(q1Radios[x].value)
    }
    }
    </script>

    <form name="the_form">
    <input type="radio" name="question1" value="1" />
    <input type="radio" name="question1" value="2" />
    <input type="radio" name="question1" value="3" />
    <br />
    <input type="button" onclick="test();" value="test" />
    </form>
    , Oct 13, 2006
    #5
  6. wreed

    wreed Guest

    wrote:
    > wreed wrote:
    > > wreed wrote:
    > > > wrote:
    > > > > What exactly are you trying to accomplish here?
    > > >
    > > > Well I am using an AHAH library (flavor of Ajax) to do my loading of
    > > > content into div's. In this case I am adding in form validation and
    > > > the way the library is written I have to do some validation this way.
    > > > Basically I am trying to tell if a radio button in the list is selected
    > > > and I want to add some HTML into a span if no radio button is selected.
    > > > Does that make sense?

    > >
    > > OOps I am basically trying to loop through each radio button list ( I
    > > have more than one) in this form elements collection.

    >
    > one way to do it would be to just loop over the radio button's
    > themselves:
    >
    > <script type="text/javascript">
    > function test() {
    > q1Radios = document.the_form.question1;
    > for (x=0; x < q1Radios.length; x++) {
    > if ( q1Radios[x].checked )
    > alert(q1Radios[x].value)
    > }
    > }
    > </script>
    >
    > <form name="the_form">
    > <input type="radio" name="question1" value="1" />
    > <input type="radio" name="question1" value="2" />
    > <input type="radio" name="question1" value="3" />
    > <br />
    > <input type="button" onclick="test();" value="test" />
    > </form>


    I need to reference the elements though, if I use q1Radios =
    the_form.elements it does not work anymore, I am originally looping
    through the elements and need to reference the radio buttons as an
    element.
    wreed, Oct 13, 2006
    #6
  7. wreed

    Guest


    > >
    > > one way to do it would be to just loop over the radio button's
    > > themselves:
    > >
    > > <script type="text/javascript">
    > > function test() {
    > > q1Radios = document.the_form.question1;
    > > for (x=0; x < q1Radios.length; x++) {
    > > if ( q1Radios[x].checked )
    > > alert(q1Radios[x].value)
    > > }
    > > }
    > > </script>
    > >
    > > <form name="the_form">
    > > <input type="radio" name="question1" value="1" />
    > > <input type="radio" name="question1" value="2" />
    > > <input type="radio" name="question1" value="3" />
    > > <br />
    > > <input type="button" onclick="test();" value="test" />
    > > </form>

    >
    > I need to reference the elements though, if I use q1Radios =
    > the_form.elements it does not work anymore, I am originally looping
    > through the elements and need to reference the radio buttons as an
    > element.


    the code i posted does reference the radio buttons as elements, i guess
    i dont understand why it has to be processed while looping thru all of
    the form elements
    , Oct 13, 2006
    #7
  8. wreed

    wreed Guest

    wrote:
    > > >
    > > > one way to do it would be to just loop over the radio button's
    > > > themselves:
    > > >
    > > > <script type="text/javascript">
    > > > function test() {
    > > > q1Radios = document.the_form.question1;
    > > > for (x=0; x < q1Radios.length; x++) {
    > > > if ( q1Radios[x].checked )
    > > > alert(q1Radios[x].value)
    > > > }
    > > > }
    > > > </script>
    > > >
    > > > <form name="the_form">
    > > > <input type="radio" name="question1" value="1" />
    > > > <input type="radio" name="question1" value="2" />
    > > > <input type="radio" name="question1" value="3" />
    > > > <br />
    > > > <input type="button" onclick="test();" value="test" />
    > > > </form>

    > >
    > > I need to reference the elements though, if I use q1Radios =
    > > the_form.elements it does not work anymore, I am originally looping
    > > through the elements and need to reference the radio buttons as an
    > > element.

    >
    > the code i posted does reference the radio buttons as elements, i guess
    > i dont understand why it has to be processed while looping thru all of
    > the form elements


    I cannot hardcode the radio button id is what I mean I need to
    reference it as elements I am trying to make this code scalable.
    q1Radios = document.the_form.question1;
    wreed, Oct 13, 2006
    #8
  9. wreed

    Guest

    > I cannot hardcode the radio button id is what I mean I need to
    > reference it as elements I am trying to make this code scalable.
    > q1Radios = document.the_form.question1;


    will this work for you?

    <script type="text/javascript">
    function test() {
    getRadios = document.getElementsByTagName("input")
    for (x=0; x < getRadios.length; x++) {
    if ( getRadios[x].type == "radio" && getRadios[x].checked )
    alert(getRadios[x].value)
    }
    }
    </script>

    <form name="the_form">
    <input type="radio" name="question1" value="1" />
    <input type="radio" name="question1" value="2" />
    <input type="radio" name="question1" value="3" />
    <br />
    <input type="button" onclick="test();" value="test" />
    </form>
    , Oct 13, 2006
    #9
  10. wreed

    wreed Guest

    wrote:
    > > I cannot hardcode the radio button id is what I mean I need to
    > > reference it as elements I am trying to make this code scalable.
    > > q1Radios = document.the_form.question1;

    >
    > will this work for you?
    >
    > <script type="text/javascript">
    > function test() {
    > getRadios = document.getElementsByTagName("input")
    > for (x=0; x < getRadios.length; x++) {
    > if ( getRadios[x].type == "radio" && getRadios[x].checked )
    > alert(getRadios[x].value)
    > }
    > }
    > </script>
    >
    > <form name="the_form">
    > <input type="radio" name="question1" value="1" />
    > <input type="radio" name="question1" value="2" />
    > <input type="radio" name="question1" value="3" />
    > <br />
    > <input type="button" onclick="test();" value="test" />
    > </form>


    This solution works great thanks, but my only thing is I lose my
    ability to have the text area's in the collection called getradios. No
    would the logical thing be to do two collections and add them together?
    i.e.
    getRadios = document.getElementsByTagName("input")
    getTextAreas = document.getElementsByTagName("textarea")

    then do some sort of getRadios.add ??? Adding collections together is
    that the right way to do it or is there a better way?
    wreed, Oct 16, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rod  Snyder

    moving from form element to form element

    Rod Snyder, May 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    405
    Vidar Petursson
    May 29, 2004
  2. Scott D Johnson
    Replies:
    1
    Views:
    1,064
    Scott D Johnson
    Sep 27, 2006
  3. Hiten
    Replies:
    1
    Views:
    620
    Vidar Petursson
    May 26, 2004
  4. laredotornado

    How to test if an element is a radio button

    laredotornado, Apr 23, 2010, in forum: Javascript
    Replies:
    3
    Views:
    85
    Thomas 'PointedEars' Lahn
    Apr 23, 2010
  5. Isaac Won
    Replies:
    9
    Views:
    371
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page