Connecting Tech Pros Worldwide Forums | Help | Site Map

How can I pass trough all objects of a form, if some of them are contained in divs?

Andrés Giraldo
Guest
 
Posts: n/a
#1: Jul 20 '05
Hi!

I'm trying to pass trough all the objects of a form but I have some text
inputs in a DIV and I have many DIVs like this on my form.

I'm doing something like:

for (i = 0; i < document.forms(0).item.length; i++) {
dosomethingwith(document.forms(0).item(i));
}

but document.forms(0).item.length are the objects that are outside the
DIVs...

How can I pass trough all the objects of a form if some of them are
contained in divs?

Thanks a Lot!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#2: Jul 20 '05

re: How can I pass trough all objects of a form, if some of them are contained in divs?


Andrés Giraldo <andres_giraldo@yahoo.com> writes:
[color=blue]
> I'm trying to pass trough all the objects of a form but I have some text
> inputs in a DIV and I have many DIVs like this on my form.[/color]

What, exactly, do you mean by "all the objects of a form"?

If you only want to access the actual form controls (input, select,
textarea elements), then what you do is close to correct:

for (var i=0;i<document.forms[0].elements.length; i++) {
dosomethingwith(document.forms[0].elements[i]);
}

Use square brackets! Both forms and elements are collections, not
functions.
[color=blue]
> for (i = 0; i < document.forms(0).item.length; i++) {
> dosomethingwith(document.forms(0).item(i));[/color]

This makes a lot of assumptions.

document.forms is a collection. It has a method called "item" that you can use to access the parts of the collection, or you can use square bracket notation.
document.forms.item(0) or document.forms[0]

However, IE shares the same object for both the forms collection and the
item function ("document.forms == document.forms.item" is true), so you
can get away with writing "document.forms(0)". Most other browsers wouldn't
accept that.

Likewise, in IE, for a form,
form == form.elements and form == form.elements.item

[color=blue]
> but document.forms(0).item.length are the objects that are outside the
> DIVs...[/color]

Huh?
The above code iterates through all form controls, and nothing else.
The div's have no influence on this at all.
[color=blue]
> How can I pass trough all the objects of a form if some of them are
> contained in divs?[/color]

Div's make no difference (unless you are using Netscape 4 and
positioned divs, which it interprets as layers and therefore separate
documents).

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Closed Thread