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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using the "count" property on radio buttons.

Status
Not open for further replies.

uksub

Programmer
Jun 15, 2003
51
US
I'm gettin an error saying that the count property as used below is not valid. Can anyone tell me what's wrong with this syntax?

For x = 0 to Document.frmMyForm.Elements.Count-1
 
An HTML form's
Code:
elements
collection doesn't have a "count" property!

You want
Code:
length
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
Code:
id
attribute set. In most cases you want to set the
Code:
id
and the
Code:
name
for every form element, and typically to the same value. The
Code:
name
is really only used for form submissions, and
Code:
id
is used by DHTML scripts and active objects (ActiveX controls, Java applets).

Most browsers will attempt to coerce the
Code:
id
to be equal to the
Code:
name
if you leave out the
Code:
id
. 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=&quot;vbscript&quot;>
      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>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top