Reputation:
Note: It is different for IE8 and IE9.
var foo = documentGetElementById( form_name ).elements;
In IE 9 foo gets an array of elements which contain values that the user submitted so that one can iterate over them like this.
for ( index in foo )
{
if( foo[index].value === 'element_value' )
{
return 1;
}
}
In IE 8 I don't know what foo has in it. But I can no longer loop through elements to pull user input from the form. IE 8 debugger reports that foo[element]
is NULL or not an object.
In both cases I don't know what it is suppose to do ( as opposed to what it actually does).
Upvotes: 0
Views: 90
Reputation: 413826
Don't use for ... in
for loops like that. Instead:
for (var index = 0; index < foo.length; ++index) {
if (foo[index].value === 'whatever') { return 1; }
}
The for ... in
form of loop is for going through properties of an Object. What you've got here is, indeed, an object, but more importantly it's something that acts more like an array. (It's a NodeList HTMLCollection, really, but for content traversal it can be treated like an array.) The advantage to iterating by numeric index is that you avoid tripping over properties of the object that really aren't part of its conceptual contents.
Your other problem was using "element" in that indexing expression; I don't know what "element" is/was but you want to use the iterator variable.
edit — The difference between what I thought the "elements" thing is (a NodeList) and what it actually is (an HTMLCollection) is that the collection API has both a numerically-indexed facade and a name-indexed facade. The thing is, if you somehow know the names external to the form, then the by-name access method makes sense. However, with what you're doing, I'd still use a numeric iterator, because otherwise things get kind-of muddy (to me). It may be that I'm just overly paranoid, so perhaps somebody with more insight will comment or answer. The way that browsers make the IDL definitions from the DOM spec work in JavaScript is an area of some flakiness, in my opinion.
Upvotes: 2