Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Radio Button Loop inside Form Element Loop

Reply
Thread Tools

Radio Button Loop inside Form Element Loop

 
 
wreed
Guest
Posts: n/a
 
      10-13-2006
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[i].type;
if (temp == "radio")
{
for (x = 0; x < the_form.elements[i].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[i].id); // returns question1
alert(the_form.question1.length); // returns 4 (amount of radio
buttons in that group)
alert(the_form.elements[i].length); // returns 0

I cannot figure out why this is the case. Am I blatantly looking over
something?

 
Reply With Quote
 
 
 
 
knizacky@gmail.com
Guest
Posts: n/a
 
      10-13-2006

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[i].type;
> if (temp == "radio")
> {
> for (x = 0; x < the_form.elements[i].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[i].id); // returns question1
> alert(the_form.question1.length); // returns 4 (amount of radio
> buttons in that group)
> alert(the_form.elements[i].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?

 
Reply With Quote
 
 
 
 
wreed
Guest
Posts: n/a
 
      10-13-2006

(E-Mail Removed) 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?

 
Reply With Quote
 
wreed
Guest
Posts: n/a
 
      10-13-2006

wreed wrote:
> (E-Mail Removed) 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.

 
Reply With Quote
 
knizacky@gmail.com
Guest
Posts: n/a
 
      10-13-2006
wreed wrote:
> wreed wrote:
> > (E-Mail Removed) 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>

 
Reply With Quote
 
wreed
Guest
Posts: n/a
 
      10-13-2006

(E-Mail Removed) wrote:
> wreed wrote:
> > wreed wrote:
> > > (E-Mail Removed) 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[i] it does not work anymore, I am originally looping
through the elements and need to reference the radio buttons as an
element.

 
Reply With Quote
 
knizacky@gmail.com
Guest
Posts: n/a
 
      10-13-2006

> >
> > 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[i] 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

 
Reply With Quote
 
wreed
Guest
Posts: n/a
 
      10-13-2006

(E-Mail Removed) 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[i] 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] I am trying to make this code scalable.
q1Radios = document.the_form.question1;

 
Reply With Quote
 
knizacky@gmail.com
Guest
Posts: n/a
 
      10-13-2006
> I cannot hardcode the radio button id is what I mean I need to
> reference it as elements[i] 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>

 
Reply With Quote
 
wreed
Guest
Posts: n/a
 
      10-16-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> > I cannot hardcode the radio button id is what I mean I need to
> > reference it as elements[i] 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?

 
Reply With Quote
 
 
 
Reply

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Button with onClick inside Form (with Submit Button) Dr. Leff Javascript 7 09-11-2007 08:05 PM
ASP.NET Reset Radio Button in a group of Radio Buttons Scott D Johnson ASP .Net 1 09-27-2006 10:38 PM
moving from form element to form element Rod Snyder ASP .Net 1 05-29-2004 01:55 PM
Radio button List problem: How to find value of Radio button list's Selected Item using javascript?? Hiten ASP .Net Web Controls 1 05-26-2004 10:32 AM



Advertisments