An HTML form's
collection doesn't have a "count" property!
You want
instead. Here's a simple example, and it presents the often-cleaner alternative of
Code:
For Each {object} In {collection}
syntax as well.
Note also that any HTML element you want to address "by name" should have its
attribute set. In most cases you want to set the
and the
for every form element, and typically to the same value. The
is really only used for form submissions, and
is used by DHTML scripts and active objects (ActiveX controls, Java applets).
Most browsers will attempt to coerce the
to be equal to the
if you leave out the
. This is imperfect however, and can lead to surprises!
Even though VBScript itself is case-insensitive it is good form to use proper case. This too can bite you if you get used to spelling things with incorrect case and then need to work with something else that requires proper case (JScript, things passed to some active objects, etc.).
sample.htm
Code:
<html>
<head>
<script language="vbscript">
Sub btnGo_onclick()
Dim x, objElem
For x = 0 To document.frmSample.elements.length - 1
MsgBox document.frmSample.elements(x).value
Next
For Each objElem In document.frmSample.elements
MsgBox objElem.value
Next
End Sub
</script>
</head>
<body>
<form id=frmSample name=frmSample>
<input type=text id=txt0 name=txt0 value=0><br>
<input type=text id=txt1 name=txt1 value=1><br>
<input type=text id=txt2 name=txt2 value=2><br>
<input type=button id=btnGo value=Go>
</form>
</body>
</html>