How to disable select field option?

Community Alums
Not applicable

We were using a script from servicenowguru to Disable/Enable Select Option.

Suddenly, after Jakarta upgrade, it stopped working.

Tried jQuery and DOM way to put it to work, but could not. Did anyone have this issue and solved it?

please share you thoughts.

find_real_file.png

In this picture, Pre-Approved must be greyed out, it was and not anymore.

7 REPLIES 7

Bryan Tay3
Mega Guru

hi Rajinis,



I tried place a 'disabled' manually on the option and it seems working.


my instance is on jakarta as well.



find_real_file.png



hope this helps.


Community Alums
Not applicable

Brian,



It works when I do inspect element and add the attribute.


But does not work on a client script.



I tried all the options..



function onLoad() {


     


      //disableOption('type', 'Pre-Approved'); // Calling UI Script



      var op = document.getElementById('change_request.type').getElementsByTagName('option');


      for (var i=0; i<op.length; i++) {


              if(op[i].value == "Pre-Approved") {


                      alert(op[i].value);


                      //op[i].setAttribute("disabled", "true");                      


                      op[i].disabled = 'true';


              }


      }


     


      //alert("Value: " + jQuery("#change_request.type option[value*='Pre-Approved']").prop('disabled',true));      


}


hi Rajinis,



you are right, seems like the OOTB change request type field option does not allowed disabled attribute.


If you really wanted this features, what I can think of is to create another field (e.g. type2) and i tested it can be disabled.


Just that you will need to link it back to the OOTB type field using on change script.



find_real_file.png


Client Script on load:



var op = document.getElementById('change_request.u_type2').getElementsByTagName('option');


      for (var i=0; i<op.length; i++) {


              if(op[i].value == "2") {


                  alert(op[i].outerHTML);        


                      op[i].disabled = 'true';


              }


      }



I am on Jakarta version well.



Hope this helps.


Community Alums
Not applicable

Hi Rajinis,



I will recommend if you don't want user to select the option in dropdown, You can use "addOption()" or "removeOption()"   functions to achieve this. It is not best practice to play with DOM. Let me know in case of any concern.