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() {
- // Gets the Student type field
- var studentType = SPUtility.GetSPField('Student Type');
- // creates a function to show or hide University field or School field
- var showOrHideField = function() {
- var studentTypeValue = studentType.GetValue();
- // Hide the City field if the selected value is Other
- if (studentTypeValue == 'University’) {
- SPUtility.HideSPField('School'); SPUtility.HideSPField('Class');
- } else {
- SPUtility.ShowSPField('School');
- SPUtility.ShowSPField('Class');
- }
- };
- // run at startup (for edit form)
- showOrHideField();
- // make sure if the user changes the value we handle it
- $(tudentType.Dropdown).on('change', showOrHideField);
- });
- </script>
- 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.
goran ljubicPosted Apr 2, 2019, 7:08 AM
I have 3 drop down field, when i set on the first drop down on "??" the second and the third drop down need to be hidden but is not.
Tejesh BarmaPosted Mar 28, 2017, 10:35 AM
Hello Chayani, Article is nice. Can you share the " $(tudentType.Dropdown)." details about this method.
Gowtham RajamanickamPosted Apr 11, 2016, 1:43 AM
please put some output window
Gowtham RajamanickamPosted Apr 11, 2016, 1:43 AM
simply awesome
Shubham KumarPosted Jan 21, 2016, 3:58 AM
nice one