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

  • Thread starter Andrés Giraldo
  • Start date
A

Andrés Giraldo

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!
 
L

Lasse Reichstein Nielsen

Andrés Giraldo said:
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.

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);
}

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

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

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

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

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

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top