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

Indexing object... simple or impossible? 1

Status
Not open for further replies.

Fredde

Programmer
May 10, 2001
22
0
0
SE
I've used ASP with javascript for a while. But I haven't
figured out how to index HTML objects so I can loop through them in a javascript.
If I for instance have four inputfields:

<INPUT name=obj1><INPUT name=obj2><INPUT name=obj3><INPUT name=obj4>

Is it possible to loop through these in any way? Or do I
have to rename them like obj[1]...obj[2]...etc?

This is an example of what I would like to do (or likewise):

<SCRIPT language=javascript>
for (i=1;i<4;i++) {
document.form.obj.visible = true
}
</SCRIPT>

I'm always told that there is no corresponding object.

thanx,
fredrik
 
must be smth like this:
Code:
<SCRIPT language=javascript>
   for (i=1;i<4;i++) {
      document.forms[formname]['obj'+i].visible = true
   }
</SCRIPT>

but that won't work anyway :)

1. there have to go obj.style.visibility
2. even that doesn't work (may be for me only.. ;)
 
you can refer to all the elements on a page by looping thru the document.all collection, this is everything on your page, including HTML, HEAD and BODY tags.
If you are not modifying elements within a form then use:

document.all.length

as this also refers to HTML BODY tags etc setting a visibility on these tags is not really good parctice, so name the elements you would like to modify with a unique prefix (get ASP to do this) and look for that prefix when looping thru the collection in javascript


otherwise as you already mentioned use:

document.YourFormName.elements.length

 
You can also do this:
Code:
for ( x = 1; x < 4; x++ ) {
   var obj = eval(&quot;document.formname.obj&quot;+x);
   obj.visible = true;
}
The only thing I'm not sure about is setting visible=true. I'm not sure that's cross-browser compatible.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
thanx for your help, this solved my problem.

/Fredrik
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top