Skip to content Skip to sidebar Skip to footer

Is It Normal For Image Inputs To Be Omitted From The Dom In Document.forms[x].elements?

I've found that given a form in a HTML page like this:

Solution 1:

It looks like that's the behavior of the elements property in all browsers.

However, you should still be able to access it through the DOM in JavaScript using the childNodes property.

For your example:

document.forms[0].childNodes.length; // equals 5 (2 inputs and 3 text nodes).document.forms[0].childNodes[1];     // This is your input with type='image'

Solution 2:

Interesting... the DOM 1 spec defines .elements as:

elements Returns a collection of all control elements in the form.

The HTML 4 spec part 17.2.1 doesn't list "image" types, so I guess that's the answer.

Solution 3:

Indeed, I see a comment: "The DOM is supposed to work that way, that's how it works in Mozilla, NS4x, and IE. We can't change that even if we wanted to, lots of sites would break." so I would lean toward an historical error. Image element is already in HTML 2 DTD...

Perhaps that's for that and possible other culprits that authors discourage using Dom hierarchy like that in favor of getElement[s]ByXxx functions (or XPath!).

Solution 4:

Been bitten by it myself. It's stated in the MSDN DHTML docs.

Post a Comment for "Is It Normal For Image Inputs To Be Omitted From The Dom In Document.forms[x].elements?"