Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

Using JavaScript with SharePoint form fields

jleung

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.

2 Comments. Leave new

[…] Continued here: Using JavaScript with SharePoint form fields […]

[…] bookmarks tagged javascript Using JavaScript with SharePoint form fields saved by 3 others     ifon1stdate bookmarked on 03/09/09 | […]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>