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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kaeli

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


    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.o ptions is an array
    of objects and should be able to use this...

    function methodTapeCheck (frm)
    {
    oddTT = frm.elements["oddTapeTyp e"];
    alert("looping with explicit index");
    for (x=0; x<oddTT.options .length; x++)
    {
    alert(oddTT.opt ions[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

    --
    ~kaeli~
    Bakers trade bread recipes on a knead-to-know basis.



  • kaeli

    #2
    Re: for...in loop kills IE6 and doesn't get option values in NN6

    In article <MPG.1a37fa68a5 c9a14e9899de@nn tp.lucent.com>,
    tiny_one@NOSPAM .comcast.net enlightened us with...[color=blue]
    >[/color]


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

    --
    ~kaeli~
    A chicken crossing the road is poultry in motion.



    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: for...in loop kills IE6 and doesn't get option values in NN6

      kaeli <tiny_one@NOSPA M.comcast.net> writes:
      [color=blue]
      > 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.[/color]

      The value of the variable is the *name* of the property, not the
      property itself.
      [color=blue]
      > Anyone know what I did wrong? AFAIK, selectElement.o ptions is an array
      > of objects and should be able to use this...[/color]

      (Continuing another thread ... no, it is not an array (as in "an
      instance created from the Array constructor"). It is an
      HTMLOptionsColl ection (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.)
      [color=blue]
      > for (var o in oddTT.options)
      > {
      > alert(o.value); // alerts "undefined" for all; kills IE6[/color]

      Use:
      alert(oddTT.opt ions[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
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...