ie javascript bug problem in this.options

Discussion in 'Javascript' started by TKapler, Aug 3, 2006.

  1. TKapler

    TKapler Guest

    i got this simmple function, It stays on onChange on select and on
    change it hides all objects with ID equal to select name plus the
    optionvalue. It works perfectly in Opera and FF, but IE crashes because
    says that options object is null

    the problem is in for (optionId in what.options)
    It seems that IE does NOT return the list of options, but only some
    other objects: language scrollheight, istextedit, currentstyle,
    document, and onmouseup

    Please can you help me how can I go throuth that cycle? I need to get
    the values of all options in select. Thank you

    function showHide (what) {
    selectedId=what.options.selectedIndex;
    selectedValue=what.options[selectedId].value;
    selectedObject = document.getElementById(what.name+selectedValue);
    selectedObject.className="";
    for (optionId in what.options)
    {
    optionValue=what.options[optionId].value;
    if (optionValue) {
    optionObject = document.getElementById(what.name+optionValue);
    if (optionId!=selectedId) {
    optionObject.className="hidden";
    }
    }
    }
    }
     
    TKapler, Aug 3, 2006
    #1
    1. Advertising

  2. On 03/08/2006 22:57, TKapler wrote:

    [snip]

    > the problem is in for (optionId in what.options)
    > It seems that IE does NOT return the list of options, but only some
    > other objects: language scrollheight, istextedit, currentstyle,
    > document, and onmouseup


    As you are dealing with a host object, it is up to the implementation to
    determine what is enumerable and what is not.

    [snip]

    Why are you using a for..in iterative statement anyway? Collections
    provide numeric indices; use a for statement, instead. Unless the real
    issue is elsewhere, you'll have better luck.

    Mike
     
    Michael Winter, Aug 3, 2006
    #2
    1. Advertising

  3. TKapler

    Guest

    TKapler wrote:
    > i got this simmple function, It stays on onChange on select and on
    > change it hides all objects with ID equal to select name plus the
    > optionvalue. It works perfectly in Opera and FF, but IE crashes because
    > says that options object is null
    >
    > the problem is in for (optionId in what.options)
    > It seems that IE does NOT return the list of options, but only some
    > other objects: language scrollheight, istextedit, currentstyle,
    > document, and onmouseup
    >
    > Please can you help me how can I go throuth that cycle? I need to get
    > the values of all options in select. Thank you
    >
    > function showHide (what) {
    > selectedId=what.options.selectedIndex;
    > selectedValue=what.options[selectedId].value;
    > selectedObject = document.getElementById(what.name+selectedValue);
    > selectedObject.className="";
    > for (optionId in what.options)
    > {
    > optionValue=what.options[optionId].value;
    > if (optionValue) {
    > optionObject = document.getElementById(what.name+optionValue);
    > if (optionId!=selectedId) {
    > optionObject.className="hidden";
    > }
    > }
    > }
    > }


    Select boxes are *themselves* a collection of options, or at least can
    be treated as one in firefox and ie:

    <select id='sbox' onclick='showmethemoney(this)'>
    <option id='1' value=0>sfljdsf</option>
    <option id='2' value=1>sfljdsf</option>
    <option id='3' value=2>sfljdsf</option>
    </select>

    <script>
    // Loop through all elements
    var sbox = document.getElementById("sbox");
    for (x=0; x < sbox.length; x++)
    {
    window.alert(sbox[x].value);
    }

    // Show which is selected
    function showmethemoney(list)
    {
    window.alert(list[list.selectedIndex].id + " is selected");
    }
    </script>

    hth

    http://darwinist.googlepages.com/htmldesktop.html
    (A free, open-source web-based IDE, windowing system, and desktop
    environment, in 31kB of html and javascript.)
     
    , Aug 4, 2006
    #3
  4. wrote:
    <snip>
    > Select boxes are *themselves* a collection of options,
    > or at least can be treated as one in firefox and ie:

    <snip>

    The ability to do something in one or two environments is not in itself
    a reason for doing it. The - options - collection of SELECT elements is
    formally specified (as a formalisation of pre-existing behaviour) so
    _any_ browser claiming to implement the W3C HTML DOM standard can be
    expected to support it (current or future). Relying on an observation of
    a few current browsers (at the cost of standardised features) is likely
    to result in code that will fail in some current browsers.

    In practice both IE and Mozilla implement the - options - collection as
    a reference to the SELECT element itself, so an attempt to use a for-in
    loop to iterate the properties of the SELECT element will be precisely
    as successful as an attempt to enumerate the properties of their -
    options - collection. And looping through the indexed properties of
    the - options - collection limited by its length property will work on
    the - options - collection, so there is no need to substitute the SELECT
    element here.

    Richard.
     
    Richard Cornford, Aug 4, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cliff  Martin
    Replies:
    1
    Views:
    3,070
    Larry Smith
    Jan 31, 2007
  2. jacksu
    Replies:
    0
    Views:
    532
    jacksu
    Oct 9, 2007
  3. Utkado
    Replies:
    2
    Views:
    2,205
  4. Bryan Richardson
    Replies:
    6
    Views:
    315
    Bryan Richardson
    Feb 25, 2008
  5. Sijo Kg
    Replies:
    1
    Views:
    108
    Justin Collins
    Jan 7, 2009
Loading...

Share This Page