Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iterating Form Elements

Status
Not open for further replies.

TazzMann

Programmer
Jul 25, 2001
79
US
I am having a Brain Freeze today. What is the easiest way in VBSCRIPT to iterate through numbered form elements.

i.e. for x = 1 to 3
test(x).style.backgroundcolor="red"
next x

Thanks in advance. Tazzmann
 
Here is my favorite method (in vb and java scripts)...

Code:
<html>
	<script language=&quot;javascript&quot;>
		function ChangeColor(){
			var z=document.forms.frmDemo	
			for (var i=0;i<z.elements.length;i++){
				z.elements[i].style.backgroundColor = &quot;#00FF00&quot;
			}		
		}
	</script>
	<script language=&quot;vbscript&quot;>
		Sub btn1_OnClick()
			Dim fld

			For Each fld in document.forms.frmDemo.elements
				fld.style.backgroundcolor = &quot;#FF0000&quot;
			Next

		End Sub
	</script>
<body>
	<form id=frmDemo>
		<input type=text id=txt1><br>
		<input type=text id=txt2><br>
		<input type=text id=txt3><br>
		<input type=text id=txt4><br>
		<input type=text id=txt5><br>
		<input type=button id=btn1 value=&quot;Click me&quot;>
		<input type=button id=btn2 value=&quot;Click me too&quot; onClick=&quot;ChangeColor();&quot;>
	</form>
</body>
</html>

Hope it helps,
Rob Rob
robschultz@yahoo.com
-Focus on the solution to the problem, not the obstacles in the way.-
 
yeah, that works good, but I need to iterate through the form objects to select just one of them. i.e. I have a form that has text boxes generated dynamically. So one time I might have three text boxes, one time I might have fifty. I can generate them with the numbers attached to their name. i.e. text1, text2, etc. But what I want to be able to do is when a user clicks on one of the text boxes, have the the textbox background color change.

If they were statically created, this would not be a problem, but since they are dynamically created, this causes a problem.

Hope this made sense. Tazzmann
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top