for...in loop kills IE6 and doesn't get option values in NN6

K

kaeli

Okay, trying to get the for...in loop syntax and obviously doing
something wrong.
I have a form and a select with options.
The explicit indexing I usually use works fine and alerts all the
values.
The for...in loop gives undefined for both IE and NN, but for IE, kills
it with way too many alerts. NN6 gives the right number of alerts (5
options, 5 alerts) but all say undefined.

Anyone know what I did wrong? AFAIK, selectElement.options is an array
of objects and should be able to use this...

function methodTapeCheck(frm)
{
oddTT = frm.elements["oddTapeType"];
alert("looping with explicit index");
for (x=0; x<oddTT.options.length; x++)
{
alert(oddTT.options[x].value); // alerts values correctly
}
alert("looping with for...in");
for (var o in oddTT.options)
{
alert(o.value); // alerts "undefined" for all; kills IE6
}
}
TIA
 
K

kaeli

Never mind. Lasse's reply to my other thread enlightened me as to why
this happened.
 
L

Lasse Reichstein Nielsen

kaeli said:
The for...in loop gives undefined for both IE and NN, but for IE, kills
it with way too many alerts. NN6 gives the right number of alerts (5
options, 5 alerts) but all say undefined.

The value of the variable is the *name* of the property, not the
property itself.
Anyone know what I did wrong? AFAIK, selectElement.options is an array
of objects and should be able to use this...

(Continuing another thread ... no, it is not an array (as in "an
instance created from the Array constructor"). It is an
HTMLOptionsCollection (see
<URL:http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html>).
It has a length property and an item method, but it lacks most other
characteristics of an actual array.)
for (var o in oddTT.options)
{
alert(o.value); // alerts "undefined" for all; kills IE6

Use:
alert(oddTT.options[o].value);

Try just:
alert(o)
to see what property names IE finds.

As I posted earlier, DOM nodes are not very good with for(in)-loops,
since there is no standard for which properties are enumerated and which
are not.

/L
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top