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

Putting controls in an array 1

Status
Not open for further replies.

snotmare

Programmer
Jan 15, 2004
262
US
Hello all! I'm fairly new to HTML, so I appreciate a good explanation.

If I have a list of controls on my web page, text boxes for example, and I want to reference them as an array, how can I group them together? Can I do this in HTML, or must it strictly be within the scripting code? (I'm most familiar with VB, but can read through JScript if necessary)

Here is a sample to work off of...
Code:
<HTML>
<HEAD>
<TITLE>File Folder Security</TITLE>
<SCRIPT TYPE="text/vbscript">
Sub subInit()
    Document.frmTest.txtTest1.Value = "test1"
    Document.frmTest.txtTest2.Value = "test2"
    Document.frmTest.txtTest3.Value = "test3"
End Sub

</SCRIPT>
</HEAD>

<BODY>
<FORM NAME="frmTest">
<INPUT TYPE="Text" NAME="txtTest1" VALUE="">
<INPUT TYPE="Text" NAME="txtTest2" VALUE="">
<INPUT TYPE="Text" NAME="txtTest3" VALUE="">
</FORM>
<SCRIPT TYPE="text/vbscript">

Call subInit()

</SCRIPT>
</BODY>
</HTML>
So, I would like to do something such as this...
Code:
Sub subInit()
    Dim i

    For i = 1 to 3
        Document.frmTest.txtTest(i).Value = "test" & i
    Next
End Sub

Thanks all for your help, any tips/pointers will be appreciated!

He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
it appears you want vbscript, but to do this in javascript, you'd do something like:

Code:
var allEls = document.forms['frmTest'].elements;
for (var i = 0; i < allEls.length; i++) {
    if (allEls[i].name.indexOf('txtTest') > -1)
        allEls[i].value = 'test' + i
}

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
For what you're trying to do, you will need scripting. The only "grouping" html can do is visually contextual by putting the input boxes in their respective fieldset. All manipulation of values for boxes must be done with scripting however. I would still advise you to think about what you're doing. If you're filling up variables with predetermined values, isn't there a better way to solve the problem. Last but not least, you might want to use JavaScript rather than VBScript, since VB is only supported in IE. So, unless you're cursed with the closed environment that needs to use IE, you should go with JavaScript for better cross browser compatibility.
 
Hello all!

Thanks for your input. I did find a solution shortly after I posted this (figures)...
Code:
For i = 1 To 3
    Document.All("txtTest" & i).Value = "test" & i
Next
The HTML doc I'm creating is strictly for inhouse, which we all use ie. However, it would be good experience to code something in jscript. I understand that ie and netscape are the two biggest browsers, but what about firefox and others that are new? Do they support jscript/vb?

Thanks!

He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
JavaScript is standard dynamic client-side scripting language in html and is as such available in all (or most) browsers (though it can also be turned off in all/most browsers). Netscape is the same as FF or Mozilla, because they are all using the same engine. VBScript (client-side) is strictly a MicroSoft thing and is supported only by their products.
 
Ok, thanks for the info! I will try to use javascript from here out. It will be hard to break from vb though :).

He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top