The CreatorCon Call for Content is officially open! Get started here.

Make all form fields read-only (client script)

brumiou
Mega Guru

Hi,

I need to put all the fields of a form read-only. I need to use a client script for that, because in certain case I will need to have some fields editable.

Is there another way than put all the fields in a ui policy (or a client script)?

Is suppose there is a way to do such a thing :

get references to all fields of the form

while(...)
{
referenced field.setreadOnly()
}

Thanks a lot

rgds

Xavier

16 REPLIES 16

SimonMorris
ServiceNow Employee
ServiceNow Employee

You can!

Use a g_form method to enumerate the editable fields on a form, and then iterate through them, setting them as read-only.



var fields = g_form.getEditableFields();
for (var x = 0; x < fields.length; x++) {
g_form.setReadOnly(fields[x], true);
}


Let me know if that works.


Hi Simon,



How about if I wanted to leave a few fields as editable and the rest as read-only?



This would help in cases where the form has 70+ fields but only about 5 need to be editable.



Any suggestions?



RV


Then after that loop that was posted above, specifically call out the fields you want to have writable and set them.



Ie: g_form.setReadOnly('field1', false); // now it's not readonly


Thank you.