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

Discussion in 'Javascript' started by Andrés Giraldo, Oct 4, 2003.

  1. 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!
     
    Andrés Giraldo, Oct 4, 2003
    #1
    1. Advertisements

  2. 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.
    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

    Huh?
    The above code iterates through all form controls, and nothing else.
    The div's have no influence on this at all.
    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, Oct 5, 2003
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.