Form images don't appear in form.elements

H

Howard Jess

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>
 
R

Richard Cornford

Howard said:
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.

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.
 

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
474,002
Messages
2,570,261
Members
46,858
Latest member
FlorrieTuf

Latest Threads

Top