Topic: Moving an <option> to the bloody top in IE (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=30735" title="Pages that link to Topic: Moving an &amp;lt;option&amp;gt; to the bloody top in IE (Page 1 of 1)" rel="nofollow" >Topic: Moving an &lt;option&gt; to the bloody top in IE <span class="small">(Page 1 of 1)</span>\

 
Maskkkk
Paranoid (IV) Inmate

From: Johnstown, PA
Insane since: Mar 2002

posted posted 12-31-2008 22:53

My client asked me (a js client side programmer) to change the order of the options so that their company's option appears at the top of a select box when the page loads.

The following code works in FF but fails in IE7 when I try to swap two options:

code:
function load{ 
                var shippingLocation  = document.getElementById("location");
                var swap = null;

                var initiallyFirstItem = shippingLocation.options[0].cloneNode(true);

                var lastPos = null;

                for(var i = 0; i < shippingLocation.length; i++) 
                {
                        if(shippingLocation.options[i].value == "XETEX")
                        { 
                                swap = shippingLocation.options[i];
                                lastPos = i;
                                break;
                        }
                }
                console.debug("sl: " + shippingLocation.options[0]);
                console.debug("s: " + swap);
                shippingLocation.options[0] = swap;
                shippingLocation.options[lastPos] = initiallyFirstItem;
                shippingLocation.selectedIndex = 0;
        }


I get an error on the next to the third line from the bottom: shippingLocation.options[0] = swap;

The error states that "object doesn't support this property or method".

What's IE's beef with switching option items?



- AIM: MASKKKK

01001101011000010111001101101011011010110110101101101011

pascarello
Obsessive-Compulsive (I) Inmate

From:
Insane since: Feb 2008

posted posted 01-05-2009 21:21

This answer is a little late, but try doing something like this:

code:
<select id="foo">
  <option value="0">A</option>
  <option value="1">B</option>
  <option value="2">C</option>
  <option value="3">D</option>
  <option value="4">E</option>
  <option value="5">F</option>
</select>


<script type="text/javascript">

  function moveOptionToTop( indx ){
    var sel = document.getElementById("foo");
    var cpy = sel.options[indx].cloneNode(true);
    sel.options[indx] = null;
    sel.insertBefore(cpy, sel.options[0]);
  }

  moveOptionToTop( 5 );
  
</script>



Eric

Blaise
Paranoid (IV) Inmate

From: London
Insane since: Jun 2003

posted posted 01-05-2009 22:37

If that works that's sexy

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

posted posted 01-05-2009 23:04

FWIW, pascarello's code looks fine to me.

coach
Nervous Wreck (II) Inmate

From:
Insane since: May 2011

posted posted 05-31-2011 11:10
Edit TP: spam removed


Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu