Using JavaScript in SharePoint forms enables developers to quickly customize the “look and field” and the behavior of a SharePoint form (Display, New or Edit form). It’s quick because you will see the effect directly on the form and given that you are embedding the JavaScript properly (putting the script in a content editor web part), you can also easily transport your code from development to production environment.
I recently have a chance to modify some SharePoint form fields thru JavaScript and would like to share my overall experience using JavaScript:
- make a text field readOnly: my first attempt is by setting the disabled property to true with no success, the correct answer is document.getElementById(‘your_control_id’).readOnly = true;
- make a checkbox field readOnly is more complicated, the readOnly property doesn’t exist/work, so I keep on researching and finally have this:
function makeReadOnly()
{
return false;
}
if (document.getElementById(‘your_control_id’).type == “checkbox”)
{
document.getElementById(‘your_control_id’).onclick = makeReadOnly;
}
- I am unable to make a textarea field read only (I will try again later)
- Setting a default value for people picker field is so far unsuccessful, I think it’s because that field is not really a plain textbox, the whole field include some javascript to validate the entry and some hidden fields as well. If setting a default value is really needed, do it thru API code instead (so create a sharepoint list item in code and set the value for the people picker field)
After thought of using JavaScript:
Works great for simple form behavior handling but if a form get really complicated e.g. data validation, dynamic value, I would suggest building an ASP.NET control or Infopath form rather than adding logic on the SharePoint Display/Edit/New forms.