tags:

views:

38

answers:

2

Hello folks,

Could someone please give me a clue as to why the following code does not work under MSIE?

<html>
<body>
<select id="si">
<option value="">1</option>
<option value="">2</option>
<option value="">3</option>
</select>

<script type="text/javascript">
var e=document.getElementById("si");
e.size=3;
e[0].style.display="none"; // <-------- no effect
</script>

</body>
</html>
+1  A: 

IE doesn't allow you to manipulate option elements directly. In my experience, you need to remove all of the option elements from the select and repopulate them with whatever changes you want to make already applied (in this case, one element removed).

inkedmn
thanks! i'll try that
diegocr
A: 

This seems to work ok under MSIE, with hopefully no "side effects", if you find something wrong please let me know it, thanks.

<html>
<body>
<select id="si">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

<script type="text/javascript">
var e=document.getElementById("si");
e.size=3;
var a = new Array();
var n = new Array();
var x = 2;
if(!a.length)for(var i=0,m=e.length;i<m;i++) {
    a.push(e[i].value);
}
while(e.firstChild)
    e.removeChild(e.firstChild);
for(var i=0,m=a.length;i<m;i++) {
    if(x==a[i]) {
        e.add(new Option(a[i],a[i]));
    }
}
</script>

</body>
</html>
diegocr