Arranging the Long List of Choice Field Values in Parallel in SharePoint Using JavaScript

Introduction

This article explains how to arrange a long list of choice field values in parallel and on the click of “Select all” for the choice fields all the choice values will be selected in SharePoint using JavaScript.

Prerequisites

  1. Ensure you have a SharePoint site.
  2. Ensure you have the following JavaScript files:
      - SPServices.0.7.2.js
      - jqueryv1.8.3.js

Use the following procedure.

Create a Custom List in SharePoint called “Test”.



Now navigate to ListList settings.

Create a new field called “States” and select the data type to be choice field and select the “checkboxes” option and click on OK.



Now navigate to the new form.aspx.

Edit the page and add the content editor webpart.



Now add a reference to jQuery JavaScript library v1.8.3 and SPServices-version 0.7.2 jQuery files.

  1. <script type="text/JavaScript" src="/ sites/Devsite /SPServices.0.7.2.js"></script>   
  2. <script type="text/JavaScript" src="/sites/Devsite/jqueryv1.8.3.js"></script>  

Now add the following lines of code.

  1. <script type="text/javascript">  
  2. _spBodyOnLoadFunctionNames.push("display");  
  3.   
  4. function display()  
  5. {  
  6.   
  7.   
  8.   $().SPServices.SPArrangeChoices({  
  9.      columnName: "States",  
  10.      perRow: 4  
  11.      });  
  12.  var oControl = findacontrol("States");  
  13.       
  14.     var inputTags = oControl.parentElement.getElementsByTagName("input");  
  15.       
  16.     for(var i=0; i<inputTags.length; i++)  
  17.     {  
  18.         var valueTag = inputTags[i].parentElement.getElementsByTagName("LABEL");  
  19.         if(valueTag[0].innerHTML.indexOf("Select All States") > -1)  
  20.         {  
  21.               
  22.             inputTags[i].attachEvent("onclick", ToggleAllStates)  
  23.         }  
  24.     }  
  25. }  
  26. function ToggleAllStates()  
  27. {  
  28.   
  29.     var oControl = findacontrol("States");  
  30.     var inputTags = oControl.parentElement.getElementsByTagName("input");  
  31.     var checkAllStates = false;  
  32.       
  33.     for(var i=0; i < inputTags.length; i++)  
  34.     {  
  35.         var valueTag = inputTags[i].parentElement.getElementsByTagName("LABEL");  
  36.         if(valueTag[0].innerHTML.indexOf("Select All States") > -1)  
  37.         {  
  38.             if(inputTags[i].checked)   
  39.             {  
  40.                 checkAllStates = true;   
  41.             }  
  42.         }  
  43.     }  
  44.     if(checkAllStates)  
  45.     {  
  46.         for(var i=0; i < inputTags.length; i++)  
  47.         {  
  48.             var valueTag = inputTags[i].parentElement.getElementsByTagName("LABEL");  
  49.             if(valueTag[0].innerHTML.indexOf("Select All States") > -1)  
  50.             {  
  51.                 inputTags[i].checked = false   
  52.             }  
  53.             else  
  54.             {     
  55.                 inputTags[i].checked = true   
  56.             }  
  57.         }  
  58.     }         
  59. }  
  60. function findacontrol(FieldName)   
  61. {  
  62.   
  63.    var arr = document.getElementsByTagName("!");  
  64.    // get all comments  
  65.    for (var i=0;i < arr.length; i++ )  
  66.    {  
  67.       // now match the field name  
  68.       if (arr[i].innerHTML.indexOf(FieldName) > 0)  
  69.       {           
  70.         return arr[i];        
  71.       }  
  72.    }  
  73. }  
  74. </script>  
Testing

Now, add a new item to the Test list.

You will be able to see the states choice values are arranged in parallel and on selection of the Select All choice all the states will be selected.



Summary

Thus in this article you saw how to arrange the long list of choice field values in paralell and on the click of the “Select all” choice fields all the choice values will be selected in SharePoint using JavaScript.

Up Next
    Ebook Download
    View all
    Learn
    View all