Topic: Moving an <option> to the bloody top in IE |
|
---|---|
Author | Thread |
Paranoid (IV) Inmate From: Johnstown, PA |
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. 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; }
|
Obsessive-Compulsive (I) Inmate From: |
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>
|
Paranoid (IV) Inmate From: London |
posted 01-05-2009 22:37 |
Paranoid (IV) Inmate From: Norway |
posted 01-05-2009 23:04 |
Nervous Wreck (II) Inmate From: |
posted 05-31-2011 11:10
Edit TP: spam removed
|