Wednesday, June 13, 2007

Using Javascript to Manipulate a List Form Field

This is something I have been doing for a while (see my nested tasks project - it has been using this trick), and now Rob Howard from Microsoft published a tip on how to use javascript to manipulate a list form field.
The example of use I would like to give is my nested tasks project- we need to set a default value when a user creates a sub task. We have a web part sitting in the "newform.aspx" page, and checks the querystring the page was opened with. If the page was opened with a querystring of "parentID=", then it loads the item that is the parent, gets the values for start date and end date, and then uses javascript to set the "Parent Task", "Start Date" and "End Date" fields.
My javascript also hides the "Parent Task" field control from the user, so he will not edit it manually. This gives us a very user friendly interface.

The same trick can be used to implement some custom validation - for example not allowing the user to enter a start date before the start date of the parent.

No comments: