Jquery Disable option in dropdown

Dead Programmer picture Dead Programmer · Feb 9, 2011 · Viewed 19.2k times · Source

Guys i have used the following code to disable an option using jQuery (jquery-1.4.2.min).The disable happens in Firefox , but not in IE.

<SELECT NAME="SCOPE" id="SCOPE">  
 <OPTION VALUE="G"> Global
 <OPTION VALUE="D"> Dynamic  
</SELECT>


 $("#SCOPE option[value='G']").attr("disabled","disabled");
 $("#SCOPE option[value='D']").attr("selected", "selected");

Answer

Val picture Val · Feb 9, 2011

I think i might be wrong but it could be because the select rather than option can be disabled. Since firefox is great and IE sucks, well you can guess why :) you having that problem. Use css to grey out the text of that option.

then on jquery do something like this.

$('#SCOPE').change(function(){
  if($('#SCOPE option[value="'+$(this).val()+'"]').attr('disabled') == 'disabled'){
    alert('Its disabled you cannot select this option');
  }
});

BTW. double check the code as I have not tested this :)