473,394 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Radio Button Loop inside Form Element Loop

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?

Oct 13 '06 #1
9 9289

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?

Oct 13 '06 #2

kniza...@gmail.com 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?

Oct 13 '06 #3

wreed wrote:
kniza...@gmail.com 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.

Oct 13 '06 #4
wreed wrote:
wreed wrote:
kniza...@gmail.com 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 '06 #5

kniza...@gmail.com wrote:
wreed wrote:
wreed wrote:
kniza...@gmail.com 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.

Oct 13 '06 #6

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

Oct 13 '06 #7

kniza...@gmail.com 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;

Oct 13 '06 #8
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>

Oct 13 '06 #9

kn******@gmail.com 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?

Oct 16 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Renie83 | last post by:
Hi all! I was wondering does anyone have any ideas on how I can send info to a SQL database by choosing a radio button. What I want to do is set one radio button to "Max" and one to type "Avg"....
15
by: JR | last post by:
Hi. I hope someone out there who is more versed with JavaScript than I can help me with the following annoying problem. Here's the problem. I have a form with the following layout: Column A...
4
by: Oscar Monteiro | last post by:
I Have to sets of Radio buttons like so: <input type="radio" name=p1 value=1> <input type="radio" name=p1 value=2> <input type="radio" name=p1 value=3> <br> <input type="radio" name=p2 value=1>...
3
by: ewitkop90 | last post by:
Here is my code: <SCRIPT> function transportchange(transport) { if (framenewinstall.Helpdesk.checked) framenewinstall.Helpdesk.checked=false; if (framenewinstall.CircuitNumber.checked)...
11
by: hygum | last post by:
I have combination of radio and select/input: http://sneleopard.dk/radio.html Is there a javascript which automatically sets radio 1, when the user does something with field 1, and automatically...
1
by: Jerry | last post by:
We have a 10-question quiz for kids, each question being a yes or no answer using radio selections. I'd like to keep a current total of yes's and no's at the bottom of the quiz (if the user selects...
7
by: nathaniel.k.lee | last post by:
Is it not possible, in IE, to dynamically click a radio button? I'm grabbing some values from a database and using them to populate radio buttons on a page. I have alternate code for Firefox...
22
by: Saul | last post by:
I have a set of radio buttons that are created dynamically, after rendered I try loop thru this set by getting the length of the set, but I keep getting an error stating the element is undefined. I...
3
by: MC | last post by:
Question, Why does the following code register the event for input type button but not radio? (And how do I make this happen?) Thanks, MC <HTML> <FORM name='x' action=''> <input type='radio'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.