How to set the default value in a SharePoint list field, based on the value in another field

sharepoint

I have a custom list in SharePoint (specifically, MOSS 2007.) One field is a yes/no checkbox titled "Any defects?" Another field is "Closed by" and names the person who has closed the ticket.

If there are no defects then I want the ticket to be auto-closed. If there are, then the "Closed by" field ought to be filled in later on.

I figured I could set a calculated default value for "Closed by" like this:

=IF([Any defects?],"",[Me])

but SharePoint complains I have referenced a field. I suppose this makes sense; the default values fire when the new list item is first opened for entry and there are no values in any fields yet.

I understand it is possible to make a calculated field based on a column value but in that case the field cannot be edited later.

Does anyone have any advice how to achieve what I am trying to do?

Is it possible to have a "OnSubmit" type event that allows me to execute some code at the point the list item is saved?

Thank you.

Best Answer

Include a content editor web part in the page (newform.aspx / editform.aspx) and use jQuery (or just plain javascript) to handle the setting of default values.

Edit: some example code:

In the lists newform.aspx, include a reference to jquery. If you look at the html code, you can see that each input tag gets an id based on the field's GUID, and a title that's set to the fields display name.

now, using jquery we can get at these fields using the jQuery selector like this:

By title:

$("input[title='DISPLAYNAMEOFFIELD']"); 

by id (if you know the field's internal guid, the dashes will ahve to be replaced by underscores:

// example field id, notice the guid and the underscores in the guid ctl00_m_g_054db6a0_0028_412d_bdc1_f2522ac3922e_ctl00_ctl04_ctl15_ctl00_ctl00_ctl04_ctl00_ctl00_TextField

$("input[id*='GUID']"); //this will get all input elements of which the id contains the specified GUID, i.e. 1 element

We wrap this in the ready() function of jQuery, so all calls will only be made when the document has fully loaded:

$(document).ready(function(){
 // enter code here, will be executed immediately after page has been loaded
}); 

By combining these 2 we could now set your dropdown's onchange event to the following

$(document).ready(function(){
 $("input[title='DISPLAYNAMEOFFIELD']").change(function() 
 {
      //do something to other field here 
 });
}); 
Related Topic