Change the display style of selection options to none in MSIE

Can someone please let me know why the following code does not work in 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>
+2
source share
3 answers

IE does not allow you to directly manipulate elements option. In my experience, you need to delete all elements optionfrom selectand re-populate them with any changes you want to apply already (in this case, one element is deleted).

+4
source

For those who deal with hidden element elements in these affected versions, I have posted a workaround here that does not clone or delete parameters, but wraps around them, which may be much easier to deal with:

http://work.arounds.org/issue/96/option-elements-do-not-hide-in-IE/

+1

This seems to work fine in MSIE, and hopefully no side effects, if you find something wrong, please let me know, 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>
0
source

Source: https://habr.com/ru/post/1796158/


All Articles