Link to home
Start Free TrialLog in
Avatar of ttomaszewicz
ttomaszewicz

asked on

<select> <option> does not accept style.display='none' under IE. Any Workaround?

Following demo code works fine for Firefox. IE6 ignores the update of style.display property.
Would you please help me?

the example code should allow the last option be selected only for the first attempt. Next time the option should be disabled.

n.b. also
document.getElementById("test").options[2].disabled=true
does not work in IE, while is OK with Firefox.
<H2>demo</h2>
<form>
<select id='test' name='test' onChange='document.getElementById("test").options[2].style.display="none"' >
<option id='test1' value='test1'>---test1---</option>
<option id='test2' value='test2'>---test2---</option>
<option id='test3' value='test3'>---test3---</option>
</select>
other inputs
</form>

Open in new window

Avatar of third
third
Flag of Philippines image

below is my suggested workaround using a global js variable.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Third Santor</title>
<script type="text/javascript">
var flag = false;
function disableOption2(obj){
  if(flag){    
    obj.options[2] = null;
  }
  flag = true;
}
</script>
</head>
<body>
<H2>demo</h2>
<form>
<select id='test' name='test' onChange='disableOption2(this);' >
<option id='test1' value='test1'>---test1---</option>
<option id='test2' value='test2'>---test2---</option>
<option id='test3' value='test3'>---test3---</option>
</select>
<input type="button" value="Click" onclick="alert(this.form.test.value)" />
other inputs
</form>
</body>
</html>

Open in new window

i added one line checking. update the script to
<script type="text/javascript">
var flag = false;
function disableOption2(obj){
  if(flag){    
    if(obj.options.length==3){
      obj.options[2] = null;
	}
  }
  flag = true;
}
</script>

Open in new window

Avatar of ttomaszewicz
ttomaszewicz

ASKER

This works! Thanks!
Is there a way to revert the last option - to be visible and selectable again? e.g. after pressing a button?
ASKER CERTIFIED SOLUTION
Avatar of third
third
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is what I needed. Thank you for your help.