Thursday 3 November 2011

Multiple default values in a column of type “Checkboxes”

I got this request from Larry:
choice – Multiple select, check boxes, is there a way to have multiple default values. I may want the same 3 items to always be selected. by default SP only allows one option. This field for 90% may need those 3 requests. Can it be done?

Add this code to a CEWP below the form in NewForm or EditForm: Alter the reference to jQuery as needed.
01<script type="text/javascript" src="../../Javascript/jquery-1.3.2.min.js"></script>
02<script type="text/javascript">
03/* Multiple default values in a column of type "Checkboxes (allow multiple selections)"
04 * Created by Alexander Bautz
05 * alexander.bautz@gmail.com
07 * v1.0
08 * LastMod: 22.02.2010
09 *
10 Sets the default value of a column of type "Checkboxes (allow multiple selections)",
11 by passing the FieldInternalName of the SharePoint-field with an array of values to prefill.
12*/
13fields = init_fields();
14// Define the array if values
15var myArr = ['Choice 4','Choice 9'];
16// Call the function with the FieldInternalName of the field and the array.
17setMultiSelectDefaultValues('MyMultiChoice',myArr);
18 
19function setMultiSelectDefaultValues(FieldInternalName,arrOfValues){
20    $(fields[FieldInternalName]).find('input:checkbox').each(function(){
21        var thisText = $(this).next().text();
22        if($.inArray(thisText,arrOfValues)>-1){
23            $(this).attr('checked',true);
24        }
25    });
26}
27 
28function init_fields(){
29  var res = {};
30  $("td.ms-formbody").each(function(){
31      if($(this).html().indexOf('FieldInternalName="')<0) return;
32      var start = $(this).html().indexOf('FieldInternalName="')+19;
33      var stopp = $(this).html().indexOf('FieldType="')-7;
34      var nm = $(this).html().substring(start,stopp);
35      res[nm] = this.parentNode;
36  });
37  return res;
38}
39</script>
You also might want to take a look at this one:
Limit number of allowed selections in Checkboxes choice

No comments:

Post a Comment