I was asked to add additional columns to a SharePoint “Out Of The Box (OOTB) calendar list with the aim of allowing the end user to complete each field in an orderly fashion.
The default field allows the user to reveal child fields based on the value of the previous field.
The fields in essence have a hierarchical structure. I decided to use a dropdown field (at least at the first two levels) to allow the user to jump from one selection to another. The last level can be any type of field. Before writing code, I executed the following steps,
- Create a “.html” file.
- Create a list (any type). In this case, I’m working with a Calendar list.
- In this list, I created four columns using the list settings. The column names are;
- “Type of Events”
- “Festivals”
- “Parties”
- “Galas”
- Add a Content Editor Web Part (CEWP) to the NewForm.aspx and EditForm.aspx files of your list.
- Edit each page, by editing the CEWP and inserting a link to your html file in the “Content Link” part of the Content Editor box and Click the “OK” or “Apply” button.
Note
You’ll require some experience with JavaScript to get through this article.
As far as the contents of the html file, I am not going to describe the css part of the code as it is not the scope of this article. You can take a look at that part of the file in more detail. The JavaScript part of the file is the most important. This is the part of the file that performs the magic. Within the <script> tag, at the top I declared my global variables as follows,
- // declare global variables
- // variables for text
- var txtTypesOfEvents = "Type of Events";
- var txtFestivals = "Festivals";
- var txtParties = "Parties";
- var txtGalas = "Galas";
- // variable for text objects
- var fldLocation = {}
- var fldTypesOfEvents = {}
- var fldFestivals = {}
- var fldParties = {}
- var fldGalas = {}
- // variables for field control objects
- var inputLocation = {}
- var selectTypesOfEvents = {}
- var selectFestivals = {}
- var selectParties = {}
- var selectGalas = {}
Now, I will use the document-ready method to execute my jQuery. The document ready event fires before all images etc. are loaded, but after the whole DOM itself is ready. The ready event occurs when the DOM (document object model) has been loaded. Because this event occurs after the document is ready, it is a good place to have all other jQuery events and functions. The ready() method specifies what happens when a ready event occurs. The ready() method should not be used together with <body onload="">.
- $(document).ready(function(){
- // declare select and input objects by title
- selectTypesOfEvents = $("select[title='" + txtTypesOfEvents + "']");
- // select field that matches txtFestivals
- selectFestivals = $("select[title='" + txtFestivals + "']");
- // select field that matches txtFestivals
- selectParties = $("select[title='" + txtParties + "']");
- // select field that matches txtParties
- inputGalas = $("input[title='" + txtGalas + "']");
- // input field that matches txtGalas
- // declare field control by table roll
- fldTypesOfEvents = selectTypesOfEvents.closest('tr');
- // roll that contains selectTypesOfEvents
- fldFestivals = selectFestivals.closest('tr');
- // roll that contains selectFestivals
- fldParties = selectParties.closest('tr');
- // roll that contains selectParties
- fldGalas = inputGalas.closest('tr');
- // roll that contains selectGalas
- // hide dropdown fields and input fields when they are empty
- if (selectFestivals.val() == "" || selectFestivals.val() == 0) {
- fldFestivals.hide();
- }
- if (selectParties.val() == "" || selectParties.val() == 0) {
- fldParties.hide();
- }
- if (inputGalas.val() == "") {
- fldGalas.hide();
- }
- // perform operations on "Types Of Events" field change
- selectTypesOfEvents.change(function() {
- if ($(this).val() == txtFestivals) { // change to "Festivals"
- fldFestivals.show();
- fldParties.hide();
- fldGalas.hide();
- }
- if ($(this).val() == txtParties) { // change to "Parties"
- fldFestivals.hide();
- fldParties.show();
- fldGalas.hide();
- }
- if ($(this).val() == txtGalas) { // change to "Galas"
- fldFestivals.hide();
- fldParties.hide();
- fldGalas.show();
- }
- });
- // perform operations on "Festivals" field change
- selectFestivals.change(function() {
- if ($(this).val() == "Wireless") {
- alert("This is an outdoor event, please come early and bring your umbrella in case it rains!");
- }
- });
- });


Larry SayteePosted Jan 22, 2020, 4:35 PM
No problem. I hope it helps!
Rushi MehtaPosted Jan 18, 2020, 2:12 PM
Thanks for sharing