Hiding and showing fields based on value selected in drop down using Jquery in SharePoint 2013.
Assumptions:
- SharePoint list called “Student”.
- A Choice field called “Student Type” (drop down) with values as ‘University’ and ‘School’.
- Some single line text fields “University”, “School”, “Class”, “Year”.
Steps:
- Open the newform.aspx page of a list.\
- Edit the page.
- Add a new script editor webpart.
- Add the below code to the script editor webpart.
- <script src="/sites/SiteAssets/jquery-1.10.2.js"></script>
- <script src="/sites/SiteAssets/sputility.min.js"></script>
-
- <script language="javascript" type="text/javascript">
- $(document).ready(function() {
-
-
- var studentType = SPUtility.GetSPField('Student Type');
-
-
- var showOrHideField = function() {
- var studentTypeValue = studentType.GetValue();
-
-
- if (studentTypeValue == 'University’) {
- SPUtility.HideSPField('School'); SPUtility.HideSPField('Class');
- } else {
- SPUtility.ShowSPField('School');
- SPUtility.ShowSPField('Class');
- }
- };
-
-
- showOrHideField();
-
-
- $(tudentType.Dropdown).on('change', showOrHideField);
- });
- </script>
Explanation:
- Reference needs to be made to jquery and SPUtility.js files.
- GetSPField(‘<parameter>’): Gets the SharePoint list field.
- GetValue(): Gets the selected value from the drop down list.
- SPUtility.HideSPField('School'): This will be hiding the fields.
- SPUtility.ShowSPField('Class'): This is be showing the fields.