473,769 Members | 2,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Radio Button Loop inside Form Element Loop

I have a for loop seen below....

var the_form = document.getEle mentById(formNa me);
for(var i=0; i<the_form.leng th; i++)
{
var temp = the_form.elemen ts[i].type;
if (temp == "radio")
{
for (x = 0; x < the_form.elemen ts[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.lengt h); // 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 9304

wreed wrote:
I have a for loop seen below....

var the_form = document.getEle mentById(formNa me);
for(var i=0; i<the_form.leng th; i++)
{
var temp = the_form.elemen ts[i].type;
if (temp == "radio")
{
for (x = 0; x < the_form.elemen ts[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.lengt h); // 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_fo rm.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_fo rm.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.elemen ts[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_fo rm.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.elemen ts[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_fo rm.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.elemen ts[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_fo rm.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_fo rm.question1;
will this work for you?

<script type="text/javascript">
function test() {
getRadios = document.getEle mentsByTagName( "input")
for (x=0; x < getRadios.lengt h; 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_fo rm.question1;

will this work for you?

<script type="text/javascript">
function test() {
getRadios = document.getEle mentsByTagName( "input")
for (x=0; x < getRadios.lengt h; 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.getEle mentsByTagName( "input")
getTextAreas = document.getEle mentsByTagName( "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
7543
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". When a user clicks on a radio button I want the value of whatever button was chosen to be placed inside the "type" field in my database after they hit submit. Also, if anyone knows of the best sites or books I can use about VBScript or ASP please...
15
2558
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 Column B Column C data 4 radio buttons more data .... ... ... .... ... ...
4
3281
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> <input type="radio" name=p2 value=2> <input type="radio" name=p2 value=3> then a text area and a button:
3
2371
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) framenewinstall.CircuitNumber.checked=false; switch (transport) { case 0: Helpdesk.innerText="No Info Needed";
11
4176
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 sets radio 2, when the user does something with field 2?
1
8856
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 yes to question 1, the total of yes's increases by 1). I've been searching for a while but I'm not sure I'm searching with the right keywords. Can anyone direct me to a source I can review to learn how to do this? Thanks! --
7
2531
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 browsers using the setAttribute() function. Everything works as planned in Firefox but in IE, the buttons won't populate and, what's worse, I can't even click on them after everything loads. I see the slight shadow that indicates you're clicking on a...
22
7978
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 am using getElelementsByName since these are radio buttons, but it seems that the dynamic element is not seen!!! This is my code... please let me know if there is anything that I am doing wrong! - thanks ---- ....
3
1314
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' name='rbTest' id='YES_1' value='1'>YES <input type='radio' name='rbTest' id='NO_1' value='0'>NO
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.