"Andy Fish" <aj****@blueyonder.co.uk> writes:
I was deliberately not giving all the detail because I think it's
clear from the context what I'm trying to do. What I want to do is
iterate through all the radio buttons in the group.
I did assume that some code was missing that would operate on the
myRadio variable. The code you presented would run with no *errors*. I
asked for how the examples failed, mostly to check what you did
expect.
I'm happy to accept that it's not an array. I understand that it
could be an object with a property called length. However, in that
case I can't see how the square bracket notation in the second
example works. I thought that square brackets applied to an object
returned the named properties.
The square bracket notation returns the value of a given property.
If "namedItem" is a property of the object "o", then "o['namedItem']"
will return the value of that property.
If you write
for (var i in o) {
... o[i] ...
}
then you will do something with the values of each property of "o".
The "for(i in obj)" construction iterates throught the names of the
enumerable properties of "obj". All the values that "i" assume are
properties of the object, but some properties are excluded (the
non-enumerable ones).
The enumerable properties of the NodeList (the radio group collection)
differs between browsers.
If I make a form (called "foo") with four radiobuttons in a group
called "bar", and use the following code to show the properties of it:
var x="";for(var i in document.forms.foo.bar) {x+=i+"\n"};alert(x)
then I get the following results.
In Mozilla, I can see the properties: item, length.
In Opera 7, I can see the properties: tags, item, namedItem.
In IE 6, I can see the properties: length, bar, bar, bar, bar. (That
is REALLY spurious, since it claims to have the same property four
times. That is, the for(..in..) construct doesn't work as it should).
In your case, you want to iterate through *some* of the properties of
the collection of radio buttons (the ones that are integers, which are
not enumerable in any browser). For that you cannot use
"for(...in...)", since it iterates through *all* the *enumerable*
properties, but you must count from 0 to length-1 manually.
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'