Form images don't appear in form.elements

Discussion in 'Javascript' started by Howard Jess, Oct 18, 2004.

  1. Howard Jess

    Howard Jess Guest

    Apparently, form elements of type

    <input type="image" src="...>

    are not included in the form's elements[] collection.

    I don't understand why not; according to DOM2, all form control
    elements in the form are part of the elements collection. Further,
    this element doesn't appear in the (DOM0) document.images
    collection either; so it's really not well-accounted for.

    And, to the contrary, the <input type="image"> element *does* have
    a 'form' property, whose value is its parent form. This seems
    completely wrong: if you count the nodes in form.elements, it
    really should be equal to the number of nodes that reference
    the form element in their form property.

    Is there an explanation for this, or was this behavior chosen
    strictly to mimic Internet Explorer's behavior? Or, am I doing
    something wrong?

    Thanks,

    hj

    Illustration:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <!-- This behavior doesn't seem to be affected if I
    omit the DTD, to fall back to quirks mode -->
    <html><head>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
    <title>Img form elements</title>
    <script type="text/javascript">
    function ecount(f) {
    var collection,i,node;
    alert(f.elements.length + " elements in form, and \n" +
    document.images.length + " images in document.images");
    collection = f.getElementsByTagName('*');
    alert(collection.length + " descendant nodes in form");
    for (i=0; i<collection.length; i++) {
    node = collection;
    alert("Form descendant node: '" + node.nodeName +
    "'.form='" + node.form + "'");
    }
    return false;
    }
    </script></head><body>
    <h1>Img form elements</h1>
    <div>
    <form action="" onsubmit="return ecount(this)">
    <p>There are 2 elements in this form:<br><br>
    &lt;input type="text"&gt;: <input type="text" name="txt" onblur="ecount(this.form)"><br>
    and &lt;input type="image"&gt;: <input type="image" src="../images/awki.png">
    </p><p>
    Click the image, or blur() the text input to count fields in the
    form's elements array.</p>
    </form></div>
    </body></html>
     
    Howard Jess, Oct 18, 2004
    #1
    1. Advertising

  2. Howard Jess wrote:
    > Apparently, form elements of type
    >
    > <input type="image" src="...>
    >
    > are not included in the form's elements[] collection.
    >
    > I don't understand why not; according to DOM2, all form
    > control elements in the form are part of the elements
    > collection.


    They don't always not appear in the elements collection, but it would be
    reasonable to say that it would not be a good idea to expect to be able
    to find them there (as they commonly will not be present).

    > Further, this element doesn't appear in the (DOM0)
    > document.images collection either;


    It would be unexpected for them to appear in - document.images - as it
    is explicitly a collection of IMG elements, no elements that happen to
    be displaying images (such as maybe OBJECT).

    > so it's really not well-accounted for.


    It is certainly a unhelpful peculiarity where forms are concerned.

    <snip>
    > Is there an explanation for this, or was this behavior
    > chosen strictly to mimic Internet Explorer's behavior?
    > Or, am I doing something wrong?

    <snip>

    I think you will find that this behaviour originates with a Netscape
    browsers. Netscape 4 had no means of referencing these elements (as they
    did not even support event handlers), and that may have also been true
    for earlier versions (thus pre-dating scriptable IE versions).

    Richard.
     
    Richard Cornford, Oct 18, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SV
    Replies:
    3
    Views:
    8,664
    Tim Ward
    Oct 20, 2004
  2. Replies:
    5
    Views:
    393
  3. PJ

    Controls don't appear - Newbie Question

    PJ, Jul 21, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    122
  4. Xeno Campanoli
    Replies:
    2
    Views:
    96
    Brian Candler
    Oct 31, 2008
  5. Hal Vaughan

    Making Form Elements Appear and Disappear

    Hal Vaughan, Dec 24, 2003, in forum: Javascript
    Replies:
    1
    Views:
    221
    Martin Honnen
    Dec 25, 2003
Loading...

Share This Page