Selecting Value of Field Based on Query String URL Using JQuery in SharePoint 2013

Assumptions:

The value to be set is passed in the query string url. Example:

<SiteURL>/sites/newform.aspx?ID=3&RollNo=10

The list has a drop down field say “ID”.
The list has a single line text field say “Roll No”.

Steps:

  1. Open the newform.aspx page of a list.
  2. Edit the page.
  3. Add a new script editor webpart.
  4. Add the below code to the script editor webpart.
  1. <script src="/sites/test/SiteAssets/jquery-1.10.2.js"></script>  
  2. < script src="/sites/test/SiteAssets/sputility.min.js"></script>  
  3.     <script language="javascript" type="text/javascript">  
  4.         $(document).ready(function()   
  5.          {  
  6.             var ID = GetUrlKeyValue('ID');  
  7.             var rollNo = GetUrlKeyValue('RollNo');  
  8.             $("Select[Title='ID']").val(ID);  
  9.             $("Select[Title='ID']").prop("disabled""disabled");  
  10.   
  11.             $("input[Title='Roll No']").val(rollNo);  
  12.             $("input[Title='Roll No']").prop("disabled""disabled");  
  13.   
  14.         });  
  15.     </script>  
Explanation:
  1. Reference needs to be made to jquery and SPUtility.js files.
  2. GetURLKeyValue(‘<parameter>’) – gets the value from the query string.
  3. $("Select[Title='ID']").val(ID); à "select" is used for drop down fields, its setting the value as per the query string value.
  4. $("input[Title='Roll No']").val(rollNo); à”input” is used for text fields and is setting the value as per the query string parameter.
  5. $("Select[Title='ID']").prop("disabled", "disabled"); à After setting the value this line, is setting the fields as disabled.