Loop finds only the first element
- From: ktangell@xxxxxxxxx
- Date: 9 Mar 2007 13:47:35 -0800
I have a webpage with multiple forms, each with a pairs of radio
buttons next to a text areas. This is an intranet application
(everyone has ie) in which users will use the radio buttons to
selectively include/exclude items from a document. I'm trying to code
a procedure to loop through the forms, look for checked radio buttons,
and output the value of related textareas to Word. The problem is, my
for..next procedure only returns the first element in the form. I've
tried this in both vbscript and javascript with the same results.
Here's a simplified version of the code:
<html>
<head>
<script language="VBScript">
sub MyTest()
dim oForms, frm
set oForms = document.forms
for each frm in oForms
for x = 0 to frm.elements.length -1
if frm.elements(x).type = "radio" then
if frm.elements(x).value = "On" then document.write
(frm.elements(x + 2).value)
end if
next
next
end sub
</script>
</head>
<body>
<input type="button" onclick="Test()">
<form name="Form1">
<table>
<tr><td>
<input type="radio" name="Radio1" value="On">
<input type="radio" name="Radio1" value="Off">
<textarea>1234567</textarea>
</td></tr>
<tr><td>
<input type="radio" name="Radio2" value="On">
<input type="radio" name="Radio2" value="Off">
<textarea>ABCDEFG</textarea>
</td></tr>
</table></form>
</body>
</html>
Ignoring the Word automation part for the moment, how do I get the
loop to read through all the elements? For what it's worth, here's
the javascript version (although I would prefer to use vbscript to
facilitate the Word automation):
<script language=javascript>
sub MyTest()
{
for (var s=0; s < document.forms.length; s++)
{
for (var i=0; i < document.forms[s].elements.length; i++)
{
getRadios = document.forms[s].elements[i];
if (getRadios.type == "radio" && getRadios.checked)
{
document.write (document.forms[s].elements[i].value);
}
}
}
}
I would appreciate any suggestions. Thanks.
.
- Follow-Ups:
- Re: Loop finds only the first element
- From: Richard Mueller [MVP]
- Re: Loop finds only the first element
- Prev by Date: Re: Reporting to excel files from multiple instances
- Next by Date: Re: Reporting to excel files from multiple instances
- Previous by thread: Re: Closeing Notepad with VBScript
- Next by thread: Re: Loop finds only the first element
- Index(es):