Show/Hide the field using client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2011 10:15 PM
There are 4 Drop Down Fields having 2 values in their list each.(Say - A,B,C,D are 4 drop down fields.'A' having the value as 1,2.'B' having the values as 3 & 4.'C' having the values as 5 & 6. 'D' having the values as 7 & 8.)Initially ,only the first 3 drop down fields will be visible(A,B,C are visible).Based on the values of those 3 drop down values,'D' field will get visible.(The values of those 3 drop down fields should be : A =2;B=4;C=6).Otherwise the 'D' field will get hidden.So.how to write the client script for this scenario???(Note:I tried with one of the field's onChange(),but it wont work properly,as it depends on the change of that field alone).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2011 02:26 AM
Didn't know the g_sc_form object yet, thank you for sharing that! 🙂
I'd suggest a small improvement, by checking the values simultaneously, as that processes quicker (and you seem to be needing them all anyway). Also note that overriding the onchange handler is discouraged by ServiceNow. Don't think this holds true for service catalog variables though, in which case this shouldn't really matter:
Note also that you are strongly discouraged from using this technique to set onSubmit(), onLoad(), or onChange() handers. Doing so will have the effect of overriding our out-of-box handlers.
Here is the code snippet:
var arr = ['A','B','C'];
if(g_sc_form.getControl(arr[0]).id != '' && g_sc_form.getControl([arr[1]).id != '' && g_sc_form.getControl([arr[2]).id){
for(var i = 0; i < arr.length; i++){
g_sc_form.getControl(arr<i>).onchange = function(event) {onChange();};
}
}
function onChange() {
if(g_form.getValue('A')==2 && g_form.getValue('B') == 2 && g_form.getValue('C')== 2){
g_form.setDisplay('D',true);
g_form.setMandatory('D',true);
} else {
g_form.setDisplay('D',false);
g_form.setMandatory('D',false);
g_form.setValue('D','');
}
}